1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/middleware/is-next-request.js
Mike Surowiec b084dbc23d React: Enable all remaining pages (#20012)
* enable all remaining pages

* update tests for survey, filter card dropdowns

* fix search test

* fix: mobile search layout

* update sidebar test

* fix: learning track page is optional

* fix broken links
2021-06-21 19:52:58 +00:00

40 lines
1.4 KiB
JavaScript

const pathToRegexp = require('path-to-regexp')
const { productIds } = require('../lib/all-products')
const versionIds = Object.keys(require('../lib/all-versions'))
const { FEATURE_NEXTJS } = process.env;
const homePageExp = pathToRegexp('/:locale/:versionId?')
const productPageExp = pathToRegexp('/:locale/:versionId?/:productId')
const subSectionExp = pathToRegexp('/:locale/:versionId?/:productId/:subSection*')
module.exports = function isNextRequest(req, res, next) {
req.renderWithNextjs = false;
if (FEATURE_NEXTJS && !req.path.startsWith('/_next/')) {
if ('nextjs' in req.query) {
req.renderWithNextjs = true;
} else {
// Custom path matching to determine if we should render with nextjs
// Remove any query string (?...) and/or fragment identifier (#...)
const { pathname } = new URL(req.originalUrl, "https://docs.github.com");
// Should the current path be rendered by NextJS?
const homePageMatch = homePageExp.exec(pathname)
const productPageMatch = productPageExp.exec(pathname)
const subSectionMatch = subSectionExp.exec(pathname)
if (homePageMatch && (!homePageMatch[2] || versionIds.includes(homePageMatch[2]))) {
req.renderWithNextjs = true
} else if (productPageMatch && productIds.includes(productPageMatch[3])) {
req.renderWithNextjs = true
} else if (subSectionMatch) {
req.renderWithNextjs = true
}
}
}
next();
};