@vercel/gatsby-plugin-vercel-builder 2.0.7 → 2.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -105,7 +105,8 @@ var import_esbuild = require("esbuild");
105
105
  var import_fs_extra2 = require("fs-extra");
106
106
  var writeHandler = async ({
107
107
  outDir,
108
- handlerFile
108
+ handlerFile,
109
+ prefix = ""
109
110
  }) => {
110
111
  const { major } = await (0, import_build_utils.getNodeVersion)(process.cwd());
111
112
  try {
@@ -119,7 +120,8 @@ var writeHandler = async ({
119
120
  bundle: true,
120
121
  minify: true,
121
122
  define: {
122
- "process.env.NODE_ENV": "'production'"
123
+ "process.env.NODE_ENV": "'production'",
124
+ vercel_pathPrefix: JSON.stringify(prefix)
123
125
  }
124
126
  });
125
127
  } catch (e) {
@@ -203,7 +205,7 @@ async function createServerlessFunctions(ssrRoutes, prefix) {
203
205
  functionDir = (0, import_path3.join)(".vercel/output/functions", functionName);
204
206
  await (0, import_fs_extra3.ensureDir)(functionDir);
205
207
  await Promise.all([
206
- writeHandler({ outDir: functionDir, handlerFile }),
208
+ writeHandler({ outDir: functionDir, handlerFile, prefix }),
207
209
  copyFunctionLibs({ functionDir }),
208
210
  copyHTMLFiles({ functionDir }),
209
211
  writeVCConfig({ functionDir })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vercel/gatsby-plugin-vercel-builder",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "main": "dist/index.js",
5
5
  "files": [
6
6
  "dist",
@@ -14,8 +14,8 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@sinclair/typebox": "0.25.24",
17
- "@vercel/build-utils": "7.2.2",
18
- "@vercel/routing-utils": "3.0.0",
17
+ "@vercel/build-utils": "7.2.3",
18
+ "@vercel/routing-utils": "3.1.0",
19
19
  "esbuild": "0.14.47",
20
20
  "etag": "1.8.1",
21
21
  "fs-extra": "11.1.0"
@@ -23,12 +23,16 @@
23
23
  "devDependencies": {
24
24
  "@types/etag": "1.8.0",
25
25
  "@types/fs-extra": "11.0.1",
26
+ "@types/jest": "27.5.1",
26
27
  "@types/node": "14.18.33",
27
28
  "@types/react": "18.0.26",
28
29
  "jest-junit": "16.0.0",
29
30
  "typescript": "4.9.5"
30
31
  },
31
32
  "scripts": {
32
- "build": "node ../../utils/build-builder.mjs"
33
+ "build": "node ../../utils/build-builder.mjs",
34
+ "test": "jest --reporters=default --reporters=jest-junit --env node --verbose --bail --runInBand",
35
+ "test-unit": "pnpm test test/unit.*test.*",
36
+ "type-check": "tsc --noEmit"
33
37
  }
34
38
  }
@@ -1,8 +1,8 @@
1
1
  import os from 'os';
2
2
  import etag from 'etag';
3
- import { parse } from 'url';
3
+ import { join } from 'path';
4
4
  import { copySync, existsSync } from 'fs-extra';
5
- import { join, dirname, basename } from 'path';
5
+ import { getPageName } from './utils';
6
6
 
7
7
  const TMP_DATA_PATH = join(os.tmpdir(), 'data/datastore');
8
8
  const CUR_DATA_PATH = join(__dirname, '.cache/data/datastore');
@@ -25,34 +25,14 @@ async function getPageSSRHelpers() {
25
25
  }
26
26
 
27
27
  export default async function handler(req, res) {
28
- let pageName;
29
- const pathname = parse(req.url).pathname || '/';
30
- const isPageData = pathname.startsWith('/page-data/');
31
- if (isPageData) {
32
- // /page-data/index/page-data.json
33
- // /page-data/using-ssr/page-data.json
34
- pageName = basename(dirname(pathname));
35
- if (pageName === 'index') {
36
- pageName = '/';
37
- }
38
- } else {
39
- // /using-ssr
40
- // /using-ssr/
41
- // /using-ssr/index.html
42
- pageName = basename(pathname);
43
- if (pageName === 'index.html') {
44
- pageName = basename(dirname(pathname));
45
- }
46
- if (!pageName) {
47
- pageName = '/';
48
- }
49
- }
28
+ // eslint-disable-next-line no-undef
29
+ const { pathName, isPageData } = getPageName(req.url, vercel_pathPrefix);
50
30
 
51
31
  const [graphqlEngine, { getData, renderHTML, renderPageData }] =
52
32
  await Promise.all([getGraphQLEngine(), getPageSSRHelpers()]);
53
33
 
54
34
  const data = await getData({
55
- pathName: pageName,
35
+ pathName,
56
36
  graphqlEngine,
57
37
  req,
58
38
  });
@@ -0,0 +1,29 @@
1
+ import { parse } from 'url';
2
+ import { basename, dirname } from 'path';
3
+
4
+ export function getPageName(url: string, pathPrefix = '') {
5
+ let pathName = (parse(url).pathname || '/').slice(pathPrefix.length);
6
+ const isPageData = pathName.startsWith('/page-data/');
7
+ if (isPageData) {
8
+ // "/page-data/index/page-data.json" -> "/"
9
+ // "/page-data/using-ssr/page-data.json" -> "using-ssr"
10
+ // "/page-data/foo/bar/ssr/page-data.json" -> "foo/bar/ssr"
11
+ pathName = pathName.split('/').slice(2, -1).join('/');
12
+ if (pathName === 'index') {
13
+ pathName = '/';
14
+ }
15
+ } else {
16
+ // "/using-ssr" -> "using-ssr"
17
+ // "/using-ssr/" -> "using-ssr"
18
+ // "/using-ssr/index.html" -> "using-ssr"
19
+ // "/foo/bar/ssr" -> "foo/bar/ssr"
20
+ if (basename(pathName) === 'index.html') {
21
+ pathName = dirname(pathName);
22
+ }
23
+ if (pathName !== '/') {
24
+ // Remove leading and trailing "/"
25
+ pathName = pathName.replace(/(^\/|\/$)/g, '');
26
+ }
27
+ }
28
+ return { isPageData, pathName };
29
+ }