Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
Fix undefined env variables errors in sitemap.xml and robots.txt
Browse files Browse the repository at this point in the history
> Trying to destructure process.env variables won't work due to
> the nature of webpack DefinePlugin.
> Source: https://nextjs.org/docs/api-reference/next.config.js/environment-variables

More info: webpack/webpack#5392 (comment)
  • Loading branch information
ruohola committed Dec 29, 2020
1 parent da20785 commit 8b59c18
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 1 addition & 3 deletions src/pages/robots.txt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { NextApiResponse } from 'next';

const { FRONTEND_URL } = process.env;

const RobotsTxt = (): void => {};

interface Props {
Expand All @@ -23,7 +21,7 @@ export const getServerSideProps = ({ res }: { res: NextApiResponse }): Props =>

const robots = `User-agent: *
${disallowed.join('\n')}
Sitemap: ${FRONTEND_URL}/sitemap.xml
Sitemap: ${process.env.FRONTEND_URL}/sitemap.xml
`;
res.setHeader('Content-Type', 'text/plain');
res.write(robots);
Expand Down
8 changes: 3 additions & 5 deletions src/pages/sitemap.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import { NextApiResponse } from 'next';
import { SitemapDocument } from 'generated';
import { initApolloClient } from 'lib';

const { FRONTEND_URL } = process.env;
const BUILD_DATE = process.env.BUILD_DATE || new Date().toISOString();

const toUrl = (path: string, modified: string): string => {
// Slice just the date portion from the ISO datetime.
const lastmod = modified ? `<lastmod>${modified.slice(0, 10)}</lastmod>` : '';

return `
<url>
<loc>${FRONTEND_URL}${path}</loc>
<loc>${process.env.FRONTEND_URL}${path}</loc>
${lastmod}
</url>`;
};
Expand All @@ -31,6 +28,7 @@ interface Props {
}

export const getServerSideProps = async ({ res }: { res: NextApiResponse }): Promise<Props> => {
const modified = process.env.BUILD_DATE || new Date().toISOString();
const staticPaths = [
'', // Don't want the index page to have a slash.
'/contact',
Expand All @@ -41,7 +39,7 @@ export const getServerSideProps = async ({ res }: { res: NextApiResponse }): Pro
'/search',
'/terms',
];
const paths = staticPaths.map((path) => ({ path, modified: BUILD_DATE }));
const paths = staticPaths.map((path) => ({ path, modified }));

const apolloClient = initApolloClient();
try {
Expand Down

0 comments on commit 8b59c18

Please sign in to comment.