mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-09 21:07:46 +00:00
[gatsby-plugin-vercel-builder] Fix nested SSR routes (#10751)
Fixes SSR / DSG pages that are nested deeper than the root path for Gatsby projects. Also introduces unit tests for the logic related to determining which page name to use.
This commit is contained in:
29
packages/gatsby-plugin-vercel-builder/templates/utils.ts
Normal file
29
packages/gatsby-plugin-vercel-builder/templates/utils.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { parse } from 'url';
|
||||
import { basename, dirname } from 'path';
|
||||
|
||||
export function getPageName(url: string, pathPrefix = '') {
|
||||
let pathName = (parse(url).pathname || '/').slice(pathPrefix.length);
|
||||
const isPageData = pathName.startsWith('/page-data/');
|
||||
if (isPageData) {
|
||||
// "/page-data/index/page-data.json" -> "/"
|
||||
// "/page-data/using-ssr/page-data.json" -> "using-ssr"
|
||||
// "/page-data/foo/bar/ssr/page-data.json" -> "foo/bar/ssr"
|
||||
pathName = pathName.split('/').slice(2, -1).join('/');
|
||||
if (pathName === 'index') {
|
||||
pathName = '/';
|
||||
}
|
||||
} else {
|
||||
// "/using-ssr" -> "using-ssr"
|
||||
// "/using-ssr/" -> "using-ssr"
|
||||
// "/using-ssr/index.html" -> "using-ssr"
|
||||
// "/foo/bar/ssr" -> "foo/bar/ssr"
|
||||
if (basename(pathName) === 'index.html') {
|
||||
pathName = dirname(pathName);
|
||||
}
|
||||
if (pathName !== '/') {
|
||||
// Remove leading and trailing "/"
|
||||
pathName = pathName.replace(/(^\/|\/$)/g, '');
|
||||
}
|
||||
}
|
||||
return { isPageData, pathName };
|
||||
}
|
||||
Reference in New Issue
Block a user