Skip to content

Commit

Permalink
Add SITE_URL env var. Use it in sitemap (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
micorix authored May 16, 2023
1 parent dff9925 commit 5ed802d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const customNextConfig = {
MAPBOX_ACCESS_TOKEN: process.env.MAPBOX_ACCESS_TOKEN,
POSTHOG_API_KEY: process.env.POSTHOG_API_KEY,
SHOW_LINKS_TO_APP: process.env.SHOW_LINKS_TO_APP,
SITE_URL: process.env.SITE_URL,
},
webpack(config) {
config.module.rules.push({
Expand Down
11 changes: 5 additions & 6 deletions src/pages/[projectID]/sitemap.xml.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { GetServerSideProps } from 'next';
import { publicRuntimeConfig } from '../../runtimeConfig';

const { API_URL } = publicRuntimeConfig;
const { API_URL, SITE_URL } = publicRuntimeConfig;

const PROJECT_LINKS = ['/', '/map', '/compare', '/calculator', '/favorites'];

const generateSiteMap = (host: string, projectId: string, rspos: string[]) => {
const generateSiteMap = (projectId: string, rspos: string[]) => {
const wrapURLWithTag = (url: string) => `<url><loc>${url}</loc></url>`;
const getFullUrl = (path: string) => `https://${host}/app/${projectId}${path}`;
const getFullUrl = (path: string) => `${SITE_URL}/app/${projectId}${path}`;
const getSchoolPath = (rspo: string) => `/school/${rspo}`;

return `<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -19,16 +19,15 @@ const generateSiteMap = (host: string, projectId: string, rspos: string[]) => {
`;
};

export const getServerSideProps: GetServerSideProps = async ({ params, res, req }) => {
const host = req.headers.host as string;
export const getServerSideProps: GetServerSideProps = async ({ params, res }) => {
const projectId = params?.projectID as string;

const schoolsForProject = await fetch(
`${API_URL}/search/institution?project_id=${projectId}`,
).then((r) => r.json());
const rspos = schoolsForProject.map((school) => school.rspo);

const sitemap = generateSiteMap(host, projectId, rspos);
const sitemap = generateSiteMap(projectId, rspos);

res.setHeader('Content-Type', 'text/xml');
res.write(sitemap);
Expand Down
2 changes: 2 additions & 0 deletions src/runtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IPublicRuntimeConfig {
MAPBOX_ACCESS_TOKEN: string;
POSTHOG_API_KEY: string;
SHOW_LINKS_TO_APP: string;
SITE_URL: string;
}

const emptyPublicRuntimeConfig: IPublicRuntimeConfig = {
Expand All @@ -19,6 +20,7 @@ const emptyPublicRuntimeConfig: IPublicRuntimeConfig = {
MAPBOX_ACCESS_TOKEN: '',
POSTHOG_API_KEY: '',
SHOW_LINKS_TO_APP: '',
SITE_URL: '',
};

export const isFeatureFlagEnabled = (flagValue: string) => flagValue === 'true';
Expand Down

0 comments on commit 5ed802d

Please sign in to comment.