Skip to content

Commit 9129bbf

Browse files
authored
chore: new constants and better analytics (#5475)
1 parent 1a3884a commit 9129bbf

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

app/sitemap.xml/route.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import { getServerSideSitemap } from 'next-sitemap';
22
import * as nextDynamic from '@/next.dynamic.mjs';
33
import * as nextConstants from '@/next.constants.mjs';
44

5-
// This is the production base domain. For the Node.js Website
6-
// @todo: We might store the "baseDomain" in the next constants and use it across the website
7-
const baseDomain = `https://nodejs.org${nextConstants.BASE_PATH}`;
5+
// This is the combination of the Application Base URL and Base PATH
6+
const canonicalUrl = `${nextConstants.BASE_URL}${nextConstants.BASE_PATH}`;
87

98
// This method populates and generates the Website Sitemap by using `next-sitemap` SSR functionality
109
// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers
@@ -23,7 +22,7 @@ export const GET = () => {
2322

2423
return getServerSideSitemap(
2524
[...dynamicRoutes, ...staticPaths].sort().map(route => ({
26-
loc: `${baseDomain}/${route}`,
25+
loc: `${canonicalUrl}/${route}`,
2726
lastmod: currentDate,
2827
changefreq: 'always',
2928
}))

components/HtmlHead.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import Head from 'next/head';
22
import { useSiteConfig } from '../hooks/useSiteConfig';
33
import { useRouter } from '../hooks/useRouter';
4+
import { BASE_URL, BASE_PATH } from '../next.constants.mjs';
45
import type { LegacyFrontMatter } from '../types';
56
import type { FC } from 'react';
67

78
type HeaderProps = { frontMatter: LegacyFrontMatter };
89

910
const HtmlHead: FC<HeaderProps> = ({ frontMatter }) => {
1011
const siteConfig = useSiteConfig();
11-
const { route, basePath } = useRouter();
12+
const { asPath, basePath } = useRouter();
1213

13-
const canonicalLink = `https://nodejs.org${route}`;
14+
const canonicalLink = `${BASE_URL}${BASE_PATH}${asPath}`;
1415

1516
const pageTitle = frontMatter.title
1617
? `${frontMatter.title} | ${siteConfig.title}`

next-data/generateWebsiteFeeds.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import * as nextConstants from '../next.constants.mjs';
1111
* @param {import('../types').BlogData} blogData
1212
*/
1313
const generateWebsiteFeeds = ({ posts }) => {
14+
const canonicalUrl = `${nextConstants.BASE_URL}${nextConstants.BASE_PATH}/en`;
15+
1416
/**
1517
* This generates all the Website RSS Feeds that are used for the website
1618
*
@@ -22,7 +24,7 @@ const generateWebsiteFeeds = ({ posts }) => {
2224
id: file,
2325
title: title,
2426
language: 'en',
25-
link: `${nextConstants.BASE_PATH}/en/feed/${file}`,
27+
link: `${canonicalUrl}/feed/${file}`,
2628
description: description || nextJson.siteConfig.description,
2729
});
2830

@@ -33,7 +35,7 @@ const generateWebsiteFeeds = ({ posts }) => {
3335
title: post.title,
3436
author: post.author,
3537
date: new Date(post.date),
36-
link: `${nextConstants.BASE_PATH}/en${post.slug}`,
38+
link: `${canonicalUrl}${post.slug}`,
3739
}));
3840

3941
blogFeedEntries.forEach(entry => feed.addItem(entry));

next.constants.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,48 @@
33
import * as nextJson from './next.json.mjs';
44
import * as nextLocales from './next.locales.mjs';
55

6+
/**
7+
* This is used for telling Next.js if the Website is deployed on Vercel
8+
*
9+
* Can be used for conditionally enabling features that we know are Vercel only
10+
*
11+
* @see https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables#framework-environment-variables
12+
*/
13+
export const VERCEL_ENV = process.env.NEXT_PUBLIC_VERCEL_ENV || undefined;
14+
615
/**
716
* This is used for telling Next.js to to a Static Export Build of the Website
817
*
918
* This is used for static/without a Node.js server hosting, such as on our
1019
* legacy Website Build Environment on Node.js's DigitalOcean Droplet.
20+
*
21+
* Note that this is a manual Environment Variable defined by us during `npm run deploy`
1122
*/
1223
export const ENABLE_STATIC_EXPORT =
1324
process.env.NEXT_STATIC_EXPORT === 'true' ||
1425
process.env.NEXT_STATIC_EXPORT === true;
1526

27+
/**
28+
* This is used for any place that requires the full canonical URL path for the Node.js Website (and its deployment), such as for example, the Node.js RSS Feed.
29+
*
30+
* This variable can either come from the Vercel Deployment as `NEXT_PUBLIC_VERCEL_URL` or from the `NEXT_BASE_URL` environment variable that is manually defined
31+
* by us if necessary. Otherwise it will fallback to the default Node.js Website URL.
32+
*
33+
* @see https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables#framework-environment-variables
34+
*/
35+
export const BASE_URL = process.env.NEXT_PUBLIC_VERCEL_URL
36+
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
37+
: process.env.NEXT_BASE_URL || 'https://nodejs.org';
38+
1639
/**
1740
* Supports a manual override of the base path of the Website
1841
*
1942
* This is useful when running the deployment on a subdirectory
2043
* of a domain, such as when hosted on GitHub Pages.
44+
*
45+
* Note that this is a manual Environment Variable defined by us if necessary.
2146
*/
22-
export const BASE_PATH = String(process.env.NEXT_BASE_PATH || '');
47+
export const BASE_PATH = process.env.NEXT_BASE_PATH || '';
2348

2449
/**
2550
* This ReGeX is used to remove the `index.md(x)` suffix of a name and to remove

pages/_app.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { Analytics } from '@vercel/analytics/react';
12
import { SiteProvider } from '../providers/siteProvider';
23
import { LocaleProvider } from '../providers/localeProvider';
34
import { BlogDataProvider } from '../providers/blogDataProvider';
45
import { NodeReleasesProvider } from '../providers/nodeReleasesProvider';
56
import { sourceSans } from '../util/nextFonts';
7+
import { VERCEL_ENV } from '../next.constants.mjs';
68
import type { AppProps } from 'next/app';
79

810
import '../styles/old/index.scss';
@@ -19,6 +21,8 @@ const App = ({ Component, pageProps }: AppProps) => (
1921
</SiteProvider>
2022
</LocaleProvider>
2123

24+
{VERCEL_ENV && <Analytics />}
25+
2226
<style jsx global>
2327
{`
2428
body {

0 commit comments

Comments
 (0)