diff --git a/.changeset/swift-kangaroos-decide.md b/.changeset/swift-kangaroos-decide.md new file mode 100644 index 000000000000..5569365c7217 --- /dev/null +++ b/.changeset/swift-kangaroos-decide.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix route matching when path includes special characters diff --git a/packages/astro/src/core/routing/match.ts b/packages/astro/src/core/routing/match.ts index cb742a5aa6b5..b7694bec2760 100644 --- a/packages/astro/src/core/routing/match.ts +++ b/packages/astro/src/core/routing/match.ts @@ -2,7 +2,7 @@ import type { ManifestData, RouteData } from '../../@types/astro'; /** Find matching route from pathname */ export function matchRoute(pathname: string, manifest: ManifestData): RouteData | undefined { - return manifest.routes.find((route) => route.pattern.test(pathname)); + return manifest.routes.find((route) => route.pattern.test(decodeURI(pathname))); } /** Find matching static asset from pathname */ diff --git a/packages/integrations/node/test/encoded.test.js b/packages/integrations/node/test/encoded.test.js new file mode 100644 index 000000000000..162ebd6cc138 --- /dev/null +++ b/packages/integrations/node/test/encoded.test.js @@ -0,0 +1,44 @@ +import nodejs from '../dist/index.js'; +import { loadFixture, createRequestAndResponse } from './test-utils.js'; +import { expect } from 'chai'; + +describe('Encoded Pathname', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/encoded/', + output: 'server', + adapter: nodejs({ mode: 'middleware' }), + }); + await fixture.build(); + }); + + it('Can get an Astro file', async () => { + const { handler } = await import('./fixtures/encoded/dist/server/entry.mjs'); + let { req, res, text } = createRequestAndResponse({ + url: '/什么', + }); + + handler(req, res); + req.send(); + + const html = await text(); + expect(html).to.include('什么'); + }); + + it('Can get a Markdown file', async () => { + const { handler } = await import('./fixtures/encoded/dist/server/entry.mjs'); + + let { req, res, text } = createRequestAndResponse({ + url: '/blog/什么', + }); + + handler(req, res); + req.send(); + + const html = await text(); + expect(html).to.include('什么'); + }); +}); diff --git a/packages/integrations/node/test/fixtures/encoded/package.json b/packages/integrations/node/test/fixtures/encoded/package.json new file mode 100644 index 000000000000..350077973049 --- /dev/null +++ b/packages/integrations/node/test/fixtures/encoded/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/nodejs-encoded", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/node": "workspace:*" + } +} diff --git "a/packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" "b/packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" new file mode 100644 index 000000000000..2820cf17ee2d --- /dev/null +++ "b/packages/integrations/node/test/fixtures/encoded/src/pages/blog/\344\273\200\344\271\210.md" @@ -0,0 +1 @@ +# 什么 diff --git "a/packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" "b/packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" new file mode 100644 index 000000000000..c8473f594ec0 --- /dev/null +++ "b/packages/integrations/node/test/fixtures/encoded/src/pages/\344\273\200\344\271\210.astro" @@ -0,0 +1 @@ +