Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url fix #39150

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Url fix #39150

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions packages/gatsby/src/bootstrap/resolve-module-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -58,7 +59,7 @@ const staticallyAnalyzeExports = (
isES6 = true
},

ExportNamedDeclaration: function ExportNamedDeclaration(astPath) {
ExportNamedDeclaration: function ExportNamedDeclaration(astPath: t.NodePath<t.ExportNamedDeclaration>) {
const declaration = astPath.node.declaration

// get foo from `export const foo = bar`
Expand All @@ -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<t.ExportSpecifier>) {
isES6 = true
const exp = astPath?.node?.exported
if (!exp) {
Expand All @@ -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<t.ExportDefaultDeclaration>) {
const declaration = astPath.node.declaration
if (
!t.isIdentifier(declaration) &&
Expand All @@ -122,7 +123,7 @@ const staticallyAnalyzeExports = (
exportNames.push(exportName)
},

AssignmentExpression: function AssignmentExpression(astPath) {
AssignmentExpression: function AssignmentExpression(astPath: t.NodePath<t.AssignmentExpression>) {
const nodeLeft = astPath.node.left

if (!t.isMemberExpression(nodeLeft)) {
Expand Down Expand Up @@ -238,3 +239,38 @@ export async function resolveModuleExports(

return []
}
import { match } from "@reach/router"

export function matchPath(rawPath: string, matchPath?: string): PathMatch {
// Normalize paths
const path = rawPath || `/`

// First try original path
let result = reachMatch(matchPath || path, { path })

// If no match and path has encoded characters
if (!result && path.includes('%')) {
try {
// Try matching decoded path
const decodedPath = decodeURIComponent(path)
result = reachMatch(matchPath || decodedPath, { path: decodedPath })

// Handle double-encoded paths
if (!result && decodedPath.includes('%')) {
const doubleDecodedPath = decodeURIComponent(decodedPath)
result = reachMatch(matchPath || doubleDecodedPath, {
path: doubleDecodedPath
})
}
} catch (e) {
// Silently handle decode errors
}
}

// Always return valid params object
return {
params: result?.params || {},
uri: result?.uri || path,
path: result?.path || path
}
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/utils/find-page-by-path.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down