diff --git a/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js b/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js index cd12905deb2e2..6b53101ae6d83 100644 --- a/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js +++ b/packages/gatsby-transformer-asciidoc/src/__tests__/gatsby-node.js @@ -1,29 +1,23 @@ -const path = require(`path`) +const fs = require('fs-extra') +const path = require('path') +const { createContentDigest } = require('gatsby-core-utils') const { onCreateNode, shouldOnCreateNode } = require(`../gatsby-node`) -jest.mock(`asciidoctor`, () => () => { - return { - load: jest.fn(() => { - return { - hasRevisionInfo: jest.fn(), - getAuthor: jest.fn(), - getAttributes: jest.fn(() => { - return {} - }), - getAttribute: jest.fn(), - convert: jest.fn(() => `html generated`), - getDocumentTitle: jest.fn(() => { - return { - getCombined: jest.fn(() => `title`), - hasSubtitle: jest.fn(() => true), - getSubtitle: jest.fn(() => `subtitle`), - getMain: jest.fn(() => `main`), - } - }), - } - }), - } -}) +jest.mock(`asciidoctor`, () => () => ({ + load: jest.fn(() => ({ + hasRevisionInfo: jest.fn(), + getAuthor: jest.fn(), + getAttributes: jest.fn(() => ({})), + getAttribute: jest.fn(), + convert: jest.fn(() => `html generated`), + getDocumentTitle: jest.fn(() => ({ + getCombined: jest.fn(() => `title`), + hasSubtitle: jest.fn(() => true), + getSubtitle: jest.fn(() => `subtitle`), + getMain: jest.fn(() => `main`), + })), + })), +})) describe(`gatsby-transformer-asciidoc`, () => { let node diff --git a/packages/gatsby/src/bootstrap/resolve-module-exports.ts b/packages/gatsby/src/bootstrap/resolve-module-exports.ts index e52b795dd4ee2..7e11a1256e8d1 100644 --- a/packages/gatsby/src/bootstrap/resolve-module-exports.ts +++ b/packages/gatsby/src/bootstrap/resolve-module-exports.ts @@ -8,6 +8,7 @@ import { testImportError } from "../utils/test-import-error" import { resolveModule, ModuleResolver } from "../utils/module-resolver" import { maybeAddFileProtocol, resolveJSFilepath } from "./resolve-js-file-path" import { preferDefault } from "./prefer-default" +import { match } from "@reach/router" const staticallyAnalyzeExports = ( modulePath: string, @@ -23,7 +24,7 @@ const staticallyAnalyzeExports = ( } const code = fs.readFileSync(absPath, `utf8`) // get file contents - let ast + let ast: t.File try { ast = babelParseToAst(code, absPath) } catch (err) { @@ -58,7 +59,7 @@ const staticallyAnalyzeExports = ( isES6 = true }, - ExportNamedDeclaration: function ExportNamedDeclaration(astPath) { + ExportNamedDeclaration: function ExportNamedDeclaration(astPath: t.NodePath) { const declaration = astPath.node.declaration // get foo from `export const foo = bar` @@ -82,7 +83,7 @@ const staticallyAnalyzeExports = ( // get foo from `export { foo } from 'bar'` // get foo from `export { foo }` - ExportSpecifier: function ExportSpecifier(astPath) { + ExportSpecifier: function ExportSpecifier(astPath: t.NodePath) { isES6 = true const exp = astPath?.node?.exported if (!exp) { @@ -100,7 +101,7 @@ const staticallyAnalyzeExports = ( // export default function() {} // export default function foo() {} // const foo = () => {}; export default foo - ExportDefaultDeclaration: function ExportDefaultDeclaration(astPath) { + ExportDefaultDeclaration: function ExportDefaultDeclaration(astPath: t.NodePath) { const declaration = astPath.node.declaration if ( !t.isIdentifier(declaration) && @@ -122,7 +123,7 @@ const staticallyAnalyzeExports = ( exportNames.push(exportName) }, - AssignmentExpression: function AssignmentExpression(astPath) { + AssignmentExpression: function AssignmentExpression(astPath: t.NodePath) { const nodeLeft = astPath.node.left if (!t.isMemberExpression(nodeLeft)) { @@ -238,3 +239,20 @@ export async function resolveModuleExports( return [] } +function matchPathParams(path: string, matchPath?: string) { + // Try original path + let result = match(matchPath || path, { path }) + + // Production SSR with encoded URL + if (!result && path.includes('%')) { + try { + const decoded = decodeURIComponent(path) + result = match(matchPath || decoded, { path: decoded }) + } catch { + // Fallback to original on decode fail + } + } + + // Never return null in SSR to prevent TypeError + return result?.params || {} +} diff --git a/packages/gatsby/src/utils/find-page-by-path.ts b/packages/gatsby/src/utils/find-page-by-path.ts index 76a248d1c6daf..64ad676301d91 100644 --- a/packages/gatsby/src/utils/find-page-by-path.ts +++ b/packages/gatsby/src/utils/find-page-by-path.ts @@ -1,5 +1,5 @@ import { IGatsbyPage, IGatsbyState } from "../redux/types" -import { pick } from "@gatsbyjs/reach-router" +import { pick } from "@reach/router" // Ranks and picks the best page to match. Each segment gets the highest // amount of points, then the type of segment gets an additional amount of