From 8921bee2e6eceb043282b624634a5cccd6d8830e Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 14 Aug 2024 10:51:58 +0100 Subject: [PATCH 1/8] feat: add `Astro.route` --- .changeset/nasty-crabs-worry.md | 25 ++++++++++++++++ packages/astro/src/core/render-context.ts | 2 ++ packages/astro/test/astro-global.test.js | 29 +++++++++++++++++++ .../astro-global/src/components/Route.astro | 4 +++ .../astro-global/src/pages/index.astro | 2 ++ .../src/pages/omit-markdown-extensions.astro | 3 ++ 6 files changed, 65 insertions(+) create mode 100644 .changeset/nasty-crabs-worry.md create mode 100644 packages/astro/test/fixtures/astro-global/src/components/Route.astro diff --git a/.changeset/nasty-crabs-worry.md b/.changeset/nasty-crabs-worry.md new file mode 100644 index 000000000000..e00c1fa7dc76 --- /dev/null +++ b/.changeset/nasty-crabs-worry.md @@ -0,0 +1,25 @@ +--- +'astro': minor +--- + +Adds a new property to the globals `Astro` and `APIContext` called `route`. The `route` represents the current route (component) +that is being rendered by Astro. It's usually a path that will look like this: `src/pages/blog/[slug].astro`: + +```asto +--- +// src/pages/index.astro +const route = Astro.route; +console.log(route); // it will log src/pages/index.astro +--- +``` + +```js +// src/pages/index.js + +export const GET = (ctx) => { + console.log(ctx.route) // it will log src/pages/index.js + return new Response.json({ loreum: "ipsum" }) +} +``` + + diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index ab0554d0c03d..74a984961d3f 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -250,6 +250,7 @@ export class RenderContext { return { cookies, + route: this.routeData.component, get clientAddress() { return renderContext.clientAddress(); }, @@ -435,6 +436,7 @@ export class RenderContext { return { generator: astroStaticPartial.generator, glob: astroStaticPartial.glob, + route: this.routeData.component, cookies, get clientAddress() { return renderContext.clientAddress(); diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index e2fbfd50bbed..d9ddc56dbdeb 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -46,6 +46,15 @@ describe('Astro Global', () => { false, ); }); + + it("Astro.route has the right value in pages and components", async () => { + let html = await fixture.fetch('/blog').then((res) => res.text()); + let $ = cheerio.load(html); + assert.match($("#route").text(), /Astro route: src\/pages\/index.astro/); + html = await fixture.fetch('/blog/omit-markdown-extensions/').then((res) => res.text()); + $ = cheerio.load(html); + assert.match($("#route").text(), /Astro route: src\/pages\/omit-markdown-extensions.astro/); + }) }); describe('build', () => { @@ -81,6 +90,15 @@ describe('Astro Global', () => { assert.equal($('[data-file]').length, 8); assert.equal($('.post-url[href]').length, 8); }); + + it("Astro.route has the right value in pages and components", async () => { + let html = await fixture.readFile('/index.html'); + let $ = cheerio.load(html); + assert.match($("#route").text(), /Astro route: src\/pages\/index.astro/); + html =await fixture.readFile('/omit-markdown-extensions/index.html'); + $ = cheerio.load(html); + assert.match($("#route").text(), /Astro route: src\/pages\/omit-markdown-extensions.astro/); + }) }); describe('app', () => { @@ -105,6 +123,17 @@ describe('Astro Global', () => { const $ = cheerio.load(html); assert.equal($('#site').attr('href'), 'https://mysite.dev/subsite/'); }); + + it("Astro.route has the right value in pages and components", async () => { + let response = await app.render(new Request('https://example.com/')); + let html = await response.text(); + let $ = cheerio.load(html); + assert.match($("#route").text(), /Astro route: src\/pages\/index.astro/); + response = await app.render(new Request('https://example.com/omit-markdown-extensions')); + html = await response.text(); + $ = cheerio.load(html); + assert.match($("#route").text(), /Astro route: src\/pages\/omit-markdown-extensions.astro/); + }) }); }); diff --git a/packages/astro/test/fixtures/astro-global/src/components/Route.astro b/packages/astro/test/fixtures/astro-global/src/components/Route.astro new file mode 100644 index 000000000000..a9f7b313e17b --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/components/Route.astro @@ -0,0 +1,4 @@ +--- +const route = Astro.route; +--- +

Astro route: {route}

diff --git a/packages/astro/test/fixtures/astro-global/src/pages/index.astro b/packages/astro/test/fixtures/astro-global/src/pages/index.astro index ad6012ce68db..6cc7be8b1ded 100644 --- a/packages/astro/test/fixtures/astro-global/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-global/src/pages/index.astro @@ -1,5 +1,6 @@ --- import Child from '../components/Child.astro'; +import Route from '../components/Route.astro'; const canonicalURL = new URL(Astro.url.pathname, Astro.site ?? `http://example.com`); --- @@ -13,5 +14,6 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site ?? `http://example.c Home + diff --git a/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro b/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro index 67f307b0f0e0..68101d447d60 100644 --- a/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro +++ b/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro @@ -1,4 +1,6 @@ --- +import Route from "../components/Route.astro"; + const markdownPosts = await Astro.glob('./post/**/*.{markdown,mdown,mkdn,mkd,mdwn,md}'); const markdownExtensions = /(\.(markdown|mdown|mkdn|mkd|mdwn|md))$/g const aUrlContainsExtension = markdownPosts.some((page:any)=> { @@ -12,5 +14,6 @@ const aUrlContainsExtension = markdownPosts.some((page:any)=> {

Placeholder

+ From be61baced7d31c619597b336159ff644873e78f0 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 16 Aug 2024 14:27:08 +0100 Subject: [PATCH 2/8] change logic and add test --- packages/astro/src/core/render-context.ts | 37 ++++++++++++++++++- packages/astro/test/astro-global.test.js | 15 +++++--- .../astro-global/src/pages/posts/[page].astro | 3 ++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index 74a984961d3f..e2ecd7b2be49 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -37,6 +37,9 @@ import { type Pipeline, Slots, getParams, getProps } from './render/index.js'; export class RenderContext { // The first route that this instance of the context attempts to render originalRoute: RouteData; + + // The component pattern to send to the users + astroRoute: string; private constructor( readonly pipeline: Pipeline, @@ -52,6 +55,7 @@ export class RenderContext { public props: Props = {}, ) { this.originalRoute = routeData; + this.astroRoute = getAstroRoute(routeData.component); } /** @@ -234,6 +238,7 @@ export class RenderContext { this.isRewriting = true; // we found a route and a component, we can change the status code to 200 this.status = 200; + this.astroRoute = getAstroRoute(routeData.component); return await this.render(component); } @@ -250,7 +255,7 @@ export class RenderContext { return { cookies, - route: this.routeData.component, + route: this.astroRoute, get clientAddress() { return renderContext.clientAddress(); }, @@ -436,7 +441,7 @@ export class RenderContext { return { generator: astroStaticPartial.generator, glob: astroStaticPartial.glob, - route: this.routeData.component, + route: this.astroRoute, cookies, get clientAddress() { return renderContext.clientAddress(); @@ -568,3 +573,31 @@ export class RenderContext { }); } } + +/** + * Return the component path without the `srcDir` and `pages` + * @param component + */ +function getAstroRoute(component: RouteData['component']): string { + let splitComponent = component.split("/"); + while (true) { + const currentPart = splitComponent.shift(); + if (!currentPart) {break} + + // "pages" isn't configurable, so it's safe to stop here + if (currentPart === "pages") { + break + } + } + + const pathWithoutPages = splitComponent.join("/"); + // This covers cases where routes don't have extensions, so they can be: [slug] or [...slug] + if (pathWithoutPages.endsWith("]")) { + return pathWithoutPages; + } + splitComponent = splitComponent.join("/").split("."); + + // this should remove the extension + splitComponent.pop(); + return splitComponent.join("/"); +} diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index d9ddc56dbdeb..cd5efa875735 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -50,10 +50,10 @@ describe('Astro Global', () => { it("Astro.route has the right value in pages and components", async () => { let html = await fixture.fetch('/blog').then((res) => res.text()); let $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: src\/pages\/index.astro/); + assert.match($("#route").text(), /Astro route: index/); html = await fixture.fetch('/blog/omit-markdown-extensions/').then((res) => res.text()); $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: src\/pages\/omit-markdown-extensions.astro/); + assert.match($("#route").text(), /Astro route: omit-markdown-extensions/); }) }); @@ -94,10 +94,13 @@ describe('Astro Global', () => { it("Astro.route has the right value in pages and components", async () => { let html = await fixture.readFile('/index.html'); let $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: src\/pages\/index.astro/); + assert.match($("#route").text(), /Astro route: index/); html =await fixture.readFile('/omit-markdown-extensions/index.html'); $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: src\/pages\/omit-markdown-extensions.astro/); + assert.match($("#route").text(), /Astro route: omit-markdown-extensions/); + html = await fixture.readFile('/posts/1/index.html'); + $ = cheerio.load(html); + assert.equal($("#route").text(), "Astro route: posts/[page]"); }) }); @@ -128,11 +131,11 @@ describe('Astro Global', () => { let response = await app.render(new Request('https://example.com/')); let html = await response.text(); let $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: src\/pages\/index.astro/); + assert.match($("#route").text(), /Astro route: index/); response = await app.render(new Request('https://example.com/omit-markdown-extensions')); html = await response.text(); $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: src\/pages\/omit-markdown-extensions.astro/); + assert.match($("#route").text(), /Astro route: omit-markdown-extensions/); }) }); }); diff --git a/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro b/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro index 0950ebe26fd9..7bdaa3b364d7 100644 --- a/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro +++ b/packages/astro/test/fixtures/astro-global/src/pages/posts/[page].astro @@ -1,4 +1,6 @@ --- +import Route from "../../components/Route.astro"; + export async function getStaticPaths({paginate}) { const data = await Astro.glob('../post/*.md'); return paginate(data, {pageSize: 1}); @@ -19,5 +21,6 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site ?? `http://example.c Read ))} + From 8df60cc382bafa14dd70bcf0e711b524cc64506a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 16 Aug 2024 14:54:23 +0100 Subject: [PATCH 3/8] rebase --- packages/astro/src/types/public/context.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/astro/src/types/public/context.ts b/packages/astro/src/types/public/context.ts index 5f05b7a66273..9c33dc1356ac 100644 --- a/packages/astro/src/types/public/context.ts +++ b/packages/astro/src/types/public/context.ts @@ -137,6 +137,16 @@ export interface AstroGlobal< * ``` */ rewrite: AstroSharedContext['rewrite']; + + /** + * The route currently rendered. It's stripped of the `srcDir` and the `pages` folder, and it doesn't contain the extension. + * + * ## Example + * - The value when rendering `src/pages/index.astro` will `index`. + * - The value when rendering `src/pages/blog/[slug].astro` will `blog/[slug]`. + * - The value when rendering `src/pages/[...path].astro` will `[...path]`. + */ + route: string; /** * The element allows a component to reference itself recursively. * @@ -498,4 +508,14 @@ export interface APIContext< * The current locale computed from the URL of the request. It matches the locales in `i18n.locales`, and returns `undefined` otherwise. */ currentLocale: string | undefined; + + /** + * The route currently rendered. It's stripped of the `srcDir` and the `pages` folder, and it doesn't contain the extension. + * + * ## Example + * - The value when rendering `src/pages/index.astro` will `index`. + * - The value when rendering `src/pages/blog/[slug].astro` will `blog/[slug]`. + * - The value when rendering `src/pages/[...path].astro` will `[...path]`. + */ + route: string } From a22de5aa844f883dd0eae5e6e4448340ed746dd1 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 16 Aug 2024 14:55:46 +0100 Subject: [PATCH 4/8] rebase --- packages/astro/src/core/middleware/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/astro/src/core/middleware/index.ts b/packages/astro/src/core/middleware/index.ts index 31988ae02eac..b54653213e04 100644 --- a/packages/astro/src/core/middleware/index.ts +++ b/packages/astro/src/core/middleware/index.ts @@ -61,6 +61,7 @@ function createContext({ generator: `Astro v${ASTRO_VERSION}`, props: {}, rewrite, + route: "", redirect(path, status) { return new Response(null, { status: status || 302, From ed8822ef675b0c1817fbe5ce17133df620c09e8b Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 20 Aug 2024 08:12:52 +0100 Subject: [PATCH 5/8] rename to `Astro.routePattern` --- .changeset/nasty-crabs-worry.md | 12 ++++++------ packages/astro/src/core/middleware/index.ts | 2 +- packages/astro/src/core/render-context.ts | 12 ++++++------ packages/astro/src/types/public/context.ts | 4 ++-- .../fixtures/astro-global/src/components/Route.astro | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.changeset/nasty-crabs-worry.md b/.changeset/nasty-crabs-worry.md index e00c1fa7dc76..10ad6ed33fdc 100644 --- a/.changeset/nasty-crabs-worry.md +++ b/.changeset/nasty-crabs-worry.md @@ -2,14 +2,14 @@ 'astro': minor --- -Adds a new property to the globals `Astro` and `APIContext` called `route`. The `route` represents the current route (component) -that is being rendered by Astro. It's usually a path that will look like this: `src/pages/blog/[slug].astro`: +Adds a new property to the globals `Astro` and `APIContext` called `routePattern`. The `routePattern` represents the current route (component) +that is being rendered by Astro. It's usually a path pattern will look like this: `blog/[slug]`: ```asto --- -// src/pages/index.astro -const route = Astro.route; -console.log(route); // it will log src/pages/index.astro +// src/pages/blog/[slug].astro +const route = Astro.routePattern; +console.log(route); // it will log "blog/[slug]" --- ``` @@ -17,7 +17,7 @@ console.log(route); // it will log src/pages/index.astro // src/pages/index.js export const GET = (ctx) => { - console.log(ctx.route) // it will log src/pages/index.js + console.log(ctx.routePattern) // it will log src/pages/index.js return new Response.json({ loreum: "ipsum" }) } ``` diff --git a/packages/astro/src/core/middleware/index.ts b/packages/astro/src/core/middleware/index.ts index b54653213e04..6364f8c9e228 100644 --- a/packages/astro/src/core/middleware/index.ts +++ b/packages/astro/src/core/middleware/index.ts @@ -61,7 +61,7 @@ function createContext({ generator: `Astro v${ASTRO_VERSION}`, props: {}, rewrite, - route: "", + routePattern: "", redirect(path, status) { return new Response(null, { status: status || 302, diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index e2ecd7b2be49..eb7c66c254dc 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -39,7 +39,7 @@ export class RenderContext { originalRoute: RouteData; // The component pattern to send to the users - astroRoute: string; + routePattern: string; private constructor( readonly pipeline: Pipeline, @@ -55,7 +55,7 @@ export class RenderContext { public props: Props = {}, ) { this.originalRoute = routeData; - this.astroRoute = getAstroRoute(routeData.component); + this.routePattern = getAstroRoutePattern(routeData.component); } /** @@ -238,7 +238,7 @@ export class RenderContext { this.isRewriting = true; // we found a route and a component, we can change the status code to 200 this.status = 200; - this.astroRoute = getAstroRoute(routeData.component); + this.routePattern = getAstroRoutePattern(routeData.component); return await this.render(component); } @@ -255,7 +255,7 @@ export class RenderContext { return { cookies, - route: this.astroRoute, + routePattern: this.routePattern, get clientAddress() { return renderContext.clientAddress(); }, @@ -441,7 +441,7 @@ export class RenderContext { return { generator: astroStaticPartial.generator, glob: astroStaticPartial.glob, - route: this.astroRoute, + routePattern: this.routePattern, cookies, get clientAddress() { return renderContext.clientAddress(); @@ -578,7 +578,7 @@ export class RenderContext { * Return the component path without the `srcDir` and `pages` * @param component */ -function getAstroRoute(component: RouteData['component']): string { +function getAstroRoutePattern(component: RouteData['component']): string { let splitComponent = component.split("/"); while (true) { const currentPart = splitComponent.shift(); diff --git a/packages/astro/src/types/public/context.ts b/packages/astro/src/types/public/context.ts index 9c33dc1356ac..e8b4f7e38be8 100644 --- a/packages/astro/src/types/public/context.ts +++ b/packages/astro/src/types/public/context.ts @@ -146,7 +146,7 @@ export interface AstroGlobal< * - The value when rendering `src/pages/blog/[slug].astro` will `blog/[slug]`. * - The value when rendering `src/pages/[...path].astro` will `[...path]`. */ - route: string; + routePattern: string; /** * The element allows a component to reference itself recursively. * @@ -517,5 +517,5 @@ export interface APIContext< * - The value when rendering `src/pages/blog/[slug].astro` will `blog/[slug]`. * - The value when rendering `src/pages/[...path].astro` will `[...path]`. */ - route: string + routePattern: string } diff --git a/packages/astro/test/fixtures/astro-global/src/components/Route.astro b/packages/astro/test/fixtures/astro-global/src/components/Route.astro index a9f7b313e17b..40adc0135438 100644 --- a/packages/astro/test/fixtures/astro-global/src/components/Route.astro +++ b/packages/astro/test/fixtures/astro-global/src/components/Route.astro @@ -1,4 +1,4 @@ --- -const route = Astro.route; +const route = Astro.routePattern; ---

Astro route: {route}

From 78343ffdb15ed64cdd95f08d3a8e9835d9ceccd5 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 21 Aug 2024 16:07:44 +0100 Subject: [PATCH 6/8] chore: added more tests --- packages/astro/test/astro-global.test.js | 30 ++++++++++++------- .../astro-global/src/components/Route.astro | 6 ++-- .../fixtures/astro-global/src/middleware.js | 8 +++++ 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 packages/astro/test/fixtures/astro-global/src/middleware.js diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index cd5efa875735..b6e434e1608f 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -47,13 +47,15 @@ describe('Astro Global', () => { ); }); - it("Astro.route has the right value in pages and components", async () => { + it("Astro.route.pattern has the right value in pages and components", async () => { let html = await fixture.fetch('/blog').then((res) => res.text()); let $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: index/); + assert.match($("#pattern").text(), /Astro route pattern: index/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: index/); html = await fixture.fetch('/blog/omit-markdown-extensions/').then((res) => res.text()); $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: omit-markdown-extensions/); + assert.match($("#pattern").text(), /Astro route pattern: omit-markdown-extensions/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: omit-markdown-extensions/); }) }); @@ -91,16 +93,22 @@ describe('Astro Global', () => { assert.equal($('.post-url[href]').length, 8); }); - it("Astro.route has the right value in pages and components", async () => { + it("Astro.route.pattern has the right value in pages and components", async () => { let html = await fixture.readFile('/index.html'); let $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: index/); + assert.match($("#pattern").text(), /Astro route pattern: index/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: index/); + html =await fixture.readFile('/omit-markdown-extensions/index.html'); $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: omit-markdown-extensions/); + assert.match($("#pattern").text(), /Astro route pattern: omit-markdown-extensions/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: omit-markdown-extensions/); + html = await fixture.readFile('/posts/1/index.html'); $ = cheerio.load(html); - assert.equal($("#route").text(), "Astro route: posts/[page]"); + assert.equal($("#pattern").text(), "Astro route pattern: posts/[page]"); + assert.equal($("#pattern-middleware").text(), "Astro route pattern middleware: posts/[page]"); + }) }); @@ -127,15 +135,17 @@ describe('Astro Global', () => { assert.equal($('#site').attr('href'), 'https://mysite.dev/subsite/'); }); - it("Astro.route has the right value in pages and components", async () => { + it("Astro.route.pattern has the right value in pages and components", async () => { let response = await app.render(new Request('https://example.com/')); let html = await response.text(); let $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: index/); + assert.match($("#pattern").text(), /Astro route pattern: index/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: index/); response = await app.render(new Request('https://example.com/omit-markdown-extensions')); html = await response.text(); $ = cheerio.load(html); - assert.match($("#route").text(), /Astro route: omit-markdown-extensions/); + assert.match($("#pattern").text(), /Astro route pattern: omit-markdown-extensions/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: omit-markdown-extensions/); }) }); }); diff --git a/packages/astro/test/fixtures/astro-global/src/components/Route.astro b/packages/astro/test/fixtures/astro-global/src/components/Route.astro index 40adc0135438..9dea56df0c08 100644 --- a/packages/astro/test/fixtures/astro-global/src/components/Route.astro +++ b/packages/astro/test/fixtures/astro-global/src/components/Route.astro @@ -1,4 +1,6 @@ --- -const route = Astro.routePattern; +const pattern = Astro.routePattern; +const localsPattern = Astro.locals.localsPattern; --- -

Astro route: {route}

+

Astro route pattern: {pattern}

+

Astro route pattern middleware: {localsPattern}

diff --git a/packages/astro/test/fixtures/astro-global/src/middleware.js b/packages/astro/test/fixtures/astro-global/src/middleware.js new file mode 100644 index 000000000000..44f0536953bc --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/middleware.js @@ -0,0 +1,8 @@ + + +export function onRequest(ctx, next) { + ctx.locals = { + localsPattern: ctx.route.pattern + }; + return next() +} From 6974a3421ceb9c36c1ee0d9852dbf756863e1346 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 21 Aug 2024 16:28:46 +0100 Subject: [PATCH 7/8] update test --- packages/astro/test/fixtures/astro-global/src/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/test/fixtures/astro-global/src/middleware.js b/packages/astro/test/fixtures/astro-global/src/middleware.js index 44f0536953bc..ee6e8a4e84c1 100644 --- a/packages/astro/test/fixtures/astro-global/src/middleware.js +++ b/packages/astro/test/fixtures/astro-global/src/middleware.js @@ -2,7 +2,7 @@ export function onRequest(ctx, next) { ctx.locals = { - localsPattern: ctx.route.pattern + localsPattern: ctx.routePattern }; return next() } From f46dc07bc6d291c54624189c0672d3ef788bf4ea Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 23 Aug 2024 16:10:38 +0100 Subject: [PATCH 8/8] add leading slash --- packages/astro/src/core/render-context.ts | 2 +- packages/astro/test/astro-global.test.js | 28 +++++++++++------------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/astro/src/core/render-context.ts b/packages/astro/src/core/render-context.ts index eb7c66c254dc..6e40c9324163 100644 --- a/packages/astro/src/core/render-context.ts +++ b/packages/astro/src/core/render-context.ts @@ -599,5 +599,5 @@ function getAstroRoutePattern(component: RouteData['component']): string { // this should remove the extension splitComponent.pop(); - return splitComponent.join("/"); + return "/" + splitComponent.join("/"); } diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index b6e434e1608f..424f8011f3bd 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -50,12 +50,12 @@ describe('Astro Global', () => { it("Astro.route.pattern has the right value in pages and components", async () => { let html = await fixture.fetch('/blog').then((res) => res.text()); let $ = cheerio.load(html); - assert.match($("#pattern").text(), /Astro route pattern: index/); - assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: index/); + assert.match($("#pattern").text(), /Astro route pattern: \/index/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \/index/); html = await fixture.fetch('/blog/omit-markdown-extensions/').then((res) => res.text()); $ = cheerio.load(html); - assert.match($("#pattern").text(), /Astro route pattern: omit-markdown-extensions/); - assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: omit-markdown-extensions/); + assert.match($("#pattern").text(), /Astro route pattern: \/omit-markdown-extensions/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \/omit-markdown-extensions/); }) }); @@ -96,18 +96,18 @@ describe('Astro Global', () => { it("Astro.route.pattern has the right value in pages and components", async () => { let html = await fixture.readFile('/index.html'); let $ = cheerio.load(html); - assert.match($("#pattern").text(), /Astro route pattern: index/); - assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: index/); + assert.match($("#pattern").text(), /Astro route pattern: \/index/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \/index/); html =await fixture.readFile('/omit-markdown-extensions/index.html'); $ = cheerio.load(html); - assert.match($("#pattern").text(), /Astro route pattern: omit-markdown-extensions/); - assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: omit-markdown-extensions/); + assert.match($("#pattern").text(), /Astro route pattern: \/omit-markdown-extensions/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \/omit-markdown-extensions/); html = await fixture.readFile('/posts/1/index.html'); $ = cheerio.load(html); - assert.equal($("#pattern").text(), "Astro route pattern: posts/[page]"); - assert.equal($("#pattern-middleware").text(), "Astro route pattern middleware: posts/[page]"); + assert.equal($("#pattern").text(), "Astro route pattern: /posts/[page]"); + assert.equal($("#pattern-middleware").text(), "Astro route pattern middleware: /posts/[page]"); }) }); @@ -139,13 +139,13 @@ describe('Astro Global', () => { let response = await app.render(new Request('https://example.com/')); let html = await response.text(); let $ = cheerio.load(html); - assert.match($("#pattern").text(), /Astro route pattern: index/); - assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: index/); + assert.match($("#pattern").text(), /Astro route pattern: \/index/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \/index/); response = await app.render(new Request('https://example.com/omit-markdown-extensions')); html = await response.text(); $ = cheerio.load(html); - assert.match($("#pattern").text(), /Astro route pattern: omit-markdown-extensions/); - assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: omit-markdown-extensions/); + assert.match($("#pattern").text(), /Astro route pattern: \/omit-markdown-extensions/); + assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \/omit-markdown-extensions/); }) }); });