Skip to content

Commit

Permalink
feat: introduced json file for redirects (nodejs#5615)
Browse files Browse the repository at this point in the history
* feat: introduced json file for redirects

* chore: remove wildcard redirects

* refactor: use the json file

* chore: removed todos

Signed-off-by: Claudio Wunder <[email protected]>

---------

Signed-off-by: Claudio Wunder <[email protected]>
  • Loading branch information
ovflowd authored Aug 10, 2023
1 parent a608e21 commit c15f704
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 237 deletions.
26 changes: 11 additions & 15 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
'use strict';

import * as nextConstants from './next.constants.mjs';
import * as nextRewrites from './next.rewrites.mjs';
import { BASE_PATH, ENABLE_STATIC_EXPORT } from './next.constants.mjs';
import { redirects, rewrites } from './next.rewrites.mjs';

/** @type {import('next').NextConfig} */
const nextConfig = {
// This configures all the Next.js rewrites, which are used for rewriting internal URLs into other internal Endpoints
// This feature is not supported within static export builds, hence we pass an empty array if static exports are enabled
rewrites: !nextConstants.ENABLE_STATIC_EXPORT
? nextRewrites.rewrites
: undefined,
// This configures all Next.js redirects
redirects: !nextConstants.ENABLE_STATIC_EXPORT
? nextRewrites.redirects
: undefined,
// We intentionally disable Next.js's built-in i18n support
// as we dom have our own i18n and internationalisation engine
i18n: null,
Expand All @@ -24,13 +15,18 @@ const nextConfig = {
trailingSlash: false,
// We allow the BASE_PATH to be overridden in case that the Website
// is being built on a subdirectory (e.g. /nodejs-website)
basePath: nextConstants.BASE_PATH,
basePath: BASE_PATH,
// We disable image optimisation during static export builds
images: { unoptimized: nextConstants.ENABLE_STATIC_EXPORT },
images: { unoptimized: ENABLE_STATIC_EXPORT },
// On static export builds we want the output directory to be "build"
distDir: nextConstants.ENABLE_STATIC_EXPORT ? 'build' : '.next',
distDir: ENABLE_STATIC_EXPORT ? 'build' : '.next',
// On static export builds we want to enable the export feature
output: nextConstants.ENABLE_STATIC_EXPORT ? 'export' : undefined,
output: ENABLE_STATIC_EXPORT ? 'export' : undefined,
// This configures all the Next.js rewrites, which are used for rewriting internal URLs into other internal Endpoints
// This feature is not supported within static export builds, hence we pass an empty array if static exports are enabled
rewrites: !ENABLE_STATIC_EXPORT ? rewrites : undefined,
// This configures all Next.js redirects
redirects: !ENABLE_STATIC_EXPORT ? redirects : undefined,
// We don't want to run Type Checking on Production Builds
// as we already check it on the CI within each Pull Request
typescript: { ignoreBuildErrors: true },
Expand Down
8 changes: 4 additions & 4 deletions next.constants.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import * as nextJson from './next.json.mjs';
import * as nextLocales from './next.locales.mjs';
import { blogData } from './next.json.mjs';
import { defaultLocale } from './next.locales.mjs';

/**
* This is used for telling Next.js if the Website is deployed on Vercel
Expand Down Expand Up @@ -80,7 +80,7 @@ export const MD_EXTENSION_REGEX = /((\/)?(index))?\.mdx?$/i;
* This should only be used outside of the Next.js Application itself
* as within React context the `useLocale` hook should be used instead.
*/
export const DEFAULT_LOCALE_CODE = nextLocales.defaultLocale.code;
export const DEFAULT_LOCALE_CODE = defaultLocale.code;

/**
* This indicates the path to the Legacy JavaScript File that is used
Expand Down Expand Up @@ -137,5 +137,5 @@ export const DYNAMIC_ROUTES_REWRITES = [
* @returns {string[]} A list of all the Dynamic Routes that are generated by the Website
*/
export const DYNAMIC_GENERATED_ROUTES = () => [
...nextJson.blogData.pagination.map(year => `en/blog/year-${year}`),
...blogData.pagination.map(year => `en/blog/year-${year}`),
];
33 changes: 13 additions & 20 deletions next.dynamic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import remarkHeadings from '@vcarl/remark-headings';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeSlug from 'rehype-slug';
import { serialize } from 'next-mdx-remote/serialize';
import * as nextLocales from './next.locales.mjs';
import * as nextConstants from './next.constants.mjs';
import * as nextHelpers from './next.helpers.mjs';
import { availableLocales } from './next.locales.mjs';
import { getMarkdownFiles } from './next.helpers.mjs';
import { DEFAULT_LOCALE_CODE, MD_EXTENSION_REGEX } from './next.constants.mjs';

// allows us to run a glob to get markdown files based on a language folder
const getPathsByLanguage = async (
locale = nextConstants.DEFAULT_LOCALE_CODE,
ignored = []
) => nextHelpers.getMarkdownFiles(process.cwd(), `pages/${locale}`, ignored);
const getPathsByLanguage = async (locale = DEFAULT_LOCALE_CODE, ignored = []) =>
getMarkdownFiles(process.cwd(), `pages/${locale}`, ignored);

/**
* This method is responsible for generating a Collection of all available paths that
Expand All @@ -31,9 +29,7 @@ const getPathsByLanguage = async (
const getAllPaths = async () => {
// during full static build we don't want to cover blog posts
// as otherwise they will all get built as static pages during build time
const sourcePages = await getPathsByLanguage(
nextConstants.DEFAULT_LOCALE_CODE
);
const sourcePages = await getPathsByLanguage(DEFAULT_LOCALE_CODE);

/**
* This method is used to provide the list of pages that are provided by a given locale
Expand All @@ -44,7 +40,7 @@ const getAllPaths = async () => {
(files = []) =>
sourcePages.map(filename => {
// remove the index.md(x) suffix from a pathname
let pathname = filename.replace(nextConstants.MD_EXTENSION_REGEX, '');
let pathname = filename.replace(MD_EXTENSION_REGEX, '');
// remove trailing slash for correct Windows pathing of the index files
if (pathname.length > 1 && pathname.endsWith(sep)) {
pathname = pathname.substring(0, pathname.length - 1);
Expand All @@ -65,11 +61,10 @@ const getAllPaths = async () => {
*
* @type {[string, import('./types').RouteSegment[]][]}
*/
const allAvailableMarkdownPaths = nextLocales.availableLocales.map(
({ code }) =>
getPathsByLanguage(code)
.then(mergePathsWithFallback(code))
.then(files => [code, files])
const allAvailableMarkdownPaths = availableLocales.map(({ code }) =>
getPathsByLanguage(code)
.then(mergePathsWithFallback(code))
.then(files => [code, files])
);

return Promise.all(allAvailableMarkdownPaths);
Expand All @@ -95,7 +90,7 @@ export const allPaths = new Map(await getAllPaths());
* @throws {Error} if the file does not exist, which should never happen
*/
export const getMarkdownFile = (
locale = nextConstants.DEFAULT_LOCALE_CODE,
locale = DEFAULT_LOCALE_CODE,
pathname = ''
) => {
const metadata = { source: '', filename: '' };
Expand All @@ -111,9 +106,7 @@ export const getMarkdownFile = (
if (route && route.filename) {
// this determines if we should be using the fallback rendering to the default locale
// or if we can use the current locale
const localeToUse = !route.localised
? nextConstants.DEFAULT_LOCALE_CODE
: locale;
const localeToUse = !route.localised ? DEFAULT_LOCALE_CODE : locale;

// gets the full pathname for the file (absolute path)
metadata.filename = join(
Expand Down
5 changes: 4 additions & 1 deletion next.json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import siteConfig from './site.json' assert { type: 'json' };
// This is the static Site Navigation (legacy website)
import siteNavigation from './navigation.json' assert { type: 'json' };

// This is the static Site External and Internal Redirects Metadata
import siteRedirects from './redirects.json' assert { type: 'json' };

// This is the Website i18n Configuration
import localeConfig from './i18n/config.json' assert { type: 'json' };

// This is the generated blog data for the Node.js Website
import blogData from './public/blog-posts-data.json' assert { type: 'json' };

export { siteConfig, siteNavigation, localeConfig, blogData };
export { siteConfig, siteNavigation, siteRedirects, localeConfig, blogData };
4 changes: 2 additions & 2 deletions next.locales.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

import * as nextJson from './next.json.mjs';
import { localeConfig } from './next.json.mjs';
import translations from './i18n/locales/index.mjs';

// As set of available and enabled locales for the website
// This is used for allowing us to redirect the user to any
// of the available locales that we have enabled on the website
const availableLocales = nextJson.localeConfig.filter(locale => locale.enabled);
const availableLocales = localeConfig.filter(locale => locale.enabled);

// This provides the default locale information for the Next.js Application
// This is marked by the unique `locale.default` property on the `en` locale
Expand Down
Loading

0 comments on commit c15f704

Please sign in to comment.