From 51f8f19959aac1c74294ea208feb0d2447cda63d Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Fri, 24 Feb 2023 07:49:08 +0000 Subject: [PATCH 01/37] =?UTF-8?q?fix=20=C2=B7=20it=20can=20access=20websit?= =?UTF-8?q?e=20without=20index=20in=20dev=20environment=20and=20production?= =?UTF-8?q?=20environment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/small-knives-sparkle.md | 5 +++++ packages/astro/src/core/build/common.ts | 6 +++++- packages/astro/src/core/routing/params.ts | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 .changeset/small-knives-sparkle.md diff --git a/.changeset/small-knives-sparkle.md b/.changeset/small-knives-sparkle.md new file mode 100644 index 000000000000..9dc0f3d75fde --- /dev/null +++ b/.changeset/small-knives-sparkle.md @@ -0,0 +1,5 @@ +--- +'astro': major +--- + +· it can access website without 'index' diff --git a/packages/astro/src/core/build/common.ts b/packages/astro/src/core/build/common.ts index 74be830f0356..5f65a6dd038c 100644 --- a/packages/astro/src/core/build/common.ts +++ b/packages/astro/src/core/build/common.ts @@ -5,6 +5,7 @@ import { appendForwardSlash } from '../../core/path.js'; const STATUS_CODE_PAGES = new Set(['/404', '/500']); const FALLBACK_OUT_DIR_NAME = './.astro/'; +const PAGEHOMENAME = 'index'; function getOutRoot(astroConfig: AstroConfig): URL { if (astroConfig.output === 'static') { @@ -29,7 +30,10 @@ export function getOutFolder( switch (astroConfig.build.format) { case 'directory': { if (STATUS_CODE_PAGES.has(pathname)) { - return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot); + if(pathname.endsWith(PAGEHOMENAME)){ + new URL('.' + appendForwardSlash(npath.dirname(pathname.slice(0, -PAGEHOMENAME.length))), outRoot);; + } + return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);; } return new URL('.' + appendForwardSlash(pathname), outRoot); } diff --git a/packages/astro/src/core/routing/params.ts b/packages/astro/src/core/routing/params.ts index 6a822861fecf..86d25278a09a 100644 --- a/packages/astro/src/core/routing/params.ts +++ b/packages/astro/src/core/routing/params.ts @@ -11,7 +11,8 @@ export function getParams(array: string[]) { const params: Params = {}; array.forEach((key, i) => { if (key.startsWith('...')) { - params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : undefined; + // default to 'index' if the param is empty in the dev environment + params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : 'index'; } else { params[key] = decodeURIComponent(match[i + 1]); } From b1e081bff052a123f3138ec7bbce69d7a8e1c6b9 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Fri, 24 Feb 2023 09:41:06 +0000 Subject: [PATCH 02/37] pass test --- packages/astro/src/core/routing/params.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/routing/params.ts b/packages/astro/src/core/routing/params.ts index 86d25278a09a..a5e5722b33c8 100644 --- a/packages/astro/src/core/routing/params.ts +++ b/packages/astro/src/core/routing/params.ts @@ -11,8 +11,7 @@ export function getParams(array: string[]) { const params: Params = {}; array.forEach((key, i) => { if (key.startsWith('...')) { - // default to 'index' if the param is empty in the dev environment - params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : 'index'; + params[key.slice(3)] = match[i + 1] ? decodeURIComponent(match[i + 1]) : undefined; } else { params[key] = decodeURIComponent(match[i + 1]); } @@ -33,7 +32,8 @@ export function stringifyParams(params: GetStaticPathsItem['params'], routeCompo const validatedParams = Object.entries(params).reduce((acc, next) => { validateGetStaticPathsParameter(next, routeComponent); const [key, value] = next; - acc[key] = value?.toString(); + // default to 'index' if the param is empty in the dev environment + acc[key] = !value ? 'index' : value?.toString(); return acc; }, {} as Params); From d155a24096e3adbab1b60b4638ed7e47c478aa8a Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 28 Feb 2023 08:57:42 +0000 Subject: [PATCH 03/37] add test --- packages/astro/package.json | 2 ++ packages/astro/src/core/build/common.ts | 11 ++++++---- .../src/pages/slug-index/[...slug].astro | 22 +++++++++++++++++++ packages/astro/test/routing-priority.test.js | 8 ++++++- 4 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 packages/astro/test/fixtures/routing-priority/src/pages/slug-index/[...slug].astro diff --git a/packages/astro/package.json b/packages/astro/package.json index 339bd8d5d2eb..8bd3a47c51d2 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -92,6 +92,8 @@ "postbuild": "astro-scripts copy \"src/**/*.astro\"", "benchmark": "node test/benchmark/dev.bench.js && node test/benchmark/build.bench.js", "test:unit": "mocha --exit --timeout 30000 ./test/units/**/*.test.js", + "test:1": "mocha --exit --timeout 30000 ./test/routing-priority.test.js", + "test:unit:match": "mocha --exit --timeout 30000 ./test/units/**/*.test.js -g", "test": "pnpm run test:unit && mocha --exit --timeout 20000 --ignore **/lit-element.test.js && mocha --timeout 20000 **/lit-element.test.js", "test:match": "mocha --timeout 20000 -g", diff --git a/packages/astro/src/core/build/common.ts b/packages/astro/src/core/build/common.ts index 5f65a6dd038c..e1e63f146d7c 100644 --- a/packages/astro/src/core/build/common.ts +++ b/packages/astro/src/core/build/common.ts @@ -29,11 +29,14 @@ export function getOutFolder( case 'page': switch (astroConfig.build.format) { case 'directory': { + if (pathname.endsWith(PAGEHOMENAME)) { + return new URL( + '.' + appendForwardSlash(npath.dirname(pathname.slice(0, -PAGEHOMENAME.length + 1))), + outRoot + ); + } if (STATUS_CODE_PAGES.has(pathname)) { - if(pathname.endsWith(PAGEHOMENAME)){ - new URL('.' + appendForwardSlash(npath.dirname(pathname.slice(0, -PAGEHOMENAME.length))), outRoot);; - } - return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);; + return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot); } return new URL('.' + appendForwardSlash(pathname), outRoot); } diff --git a/packages/astro/test/fixtures/routing-priority/src/pages/slug-index/[...slug].astro b/packages/astro/test/fixtures/routing-priority/src/pages/slug-index/[...slug].astro new file mode 100644 index 000000000000..0bfcf4b3d3ff --- /dev/null +++ b/packages/astro/test/fixtures/routing-priority/src/pages/slug-index/[...slug].astro @@ -0,0 +1,22 @@ +--- +export function getStaticPaths() { + return [{ + params: { slug: "index" }, + }, { + params: { slug: 'potato' }, + }] +} +const { slug } = Astro.params +--- + + + + + + {slug} + + +

slug-index/[...slug].astro

+

slug: {slug || 'index'}

+ + diff --git a/packages/astro/test/routing-priority.test.js b/packages/astro/test/routing-priority.test.js index 586978d4f4cd..e7c4f0612c1b 100644 --- a/packages/astro/test/routing-priority.test.js +++ b/packages/astro/test/routing-priority.test.js @@ -95,6 +95,12 @@ const routes = [ h1: '[id].astro', p: 'injected-2', }, + { + description: 'matches /slug-index to slug-index/[...slug].astro', + url: '/slug-index', + h1: 'slug-index/[...slug].astro', + p: 'slug: index', + }, { description: 'matches /empty-slug to empty-slug/[...slug].astro', url: '/empty-slug', @@ -143,7 +149,7 @@ describe('Routing priority', () => { it(description, async () => { const htmlFile = isEndpoint ? url : `${appendForwardSlash(url)}index.html`; - + debugger if (fourOhFour) { expect(fixture.pathExists(htmlFile)).to.be.false; return; From bd2c39c47f71de8e57834865e0636370091fbb10 Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Tue, 28 Feb 2023 08:59:04 +0000 Subject: [PATCH 04/37] delete redundent code --- packages/astro/package.json | 4 +--- .../astro/test/fixtures/unused-slot/src/components/Card.astro | 1 - packages/astro/test/routing-priority.test.js | 3 +-- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 8bd3a47c51d2..44da301e6919 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -91,9 +91,7 @@ "dev": "astro-scripts dev --prebuild \"src/runtime/server/astro-island.ts\" --prebuild \"src/runtime/client/{idle,load,media,only,visible}.ts\" \"src/**/*.ts\"", "postbuild": "astro-scripts copy \"src/**/*.astro\"", "benchmark": "node test/benchmark/dev.bench.js && node test/benchmark/build.bench.js", - "test:unit": "mocha --exit --timeout 30000 ./test/units/**/*.test.js", - "test:1": "mocha --exit --timeout 30000 ./test/routing-priority.test.js", - + "test:unit": "mocha --exit --timeout 30000 ./test/units/**/*.test.js", "test:unit:match": "mocha --exit --timeout 30000 ./test/units/**/*.test.js -g", "test": "pnpm run test:unit && mocha --exit --timeout 20000 --ignore **/lit-element.test.js && mocha --timeout 20000 **/lit-element.test.js", "test:match": "mocha --timeout 20000 -g", diff --git a/packages/astro/test/fixtures/unused-slot/src/components/Card.astro b/packages/astro/test/fixtures/unused-slot/src/components/Card.astro index 61fafb04c01e..c77e31672f4c 100644 --- a/packages/astro/test/fixtures/unused-slot/src/components/Card.astro +++ b/packages/astro/test/fixtures/unused-slot/src/components/Card.astro @@ -5,7 +5,6 @@ export interface Props { href: string, } const {href, title, body} = Astro.props; -debugger; ---