0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

refactor: Astro.routePattern is RouteData.route (#11867)

This commit is contained in:
Emanuele Stoppa 2024-08-29 08:46:51 +01:00 committed by GitHub
parent c7a2ded2eb
commit 3eeaf6bdeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 42 deletions

View file

@ -38,9 +38,6 @@ 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
routePattern: string;
private constructor(
readonly pipeline: Pipeline,
public locals: App.Locals,
@ -55,7 +52,6 @@ export class RenderContext {
public props: Props = {},
) {
this.originalRoute = routeData;
this.routePattern = getAstroRoutePattern(routeData.component);
}
/**
@ -264,7 +260,7 @@ export class RenderContext {
return {
cookies,
routePattern: this.routePattern,
routePattern: this.routeData.route,
get clientAddress() {
return renderContext.clientAddress();
},
@ -449,7 +445,7 @@ export class RenderContext {
return {
generator: astroStaticPartial.generator,
glob: astroStaticPartial.glob,
routePattern: this.routePattern,
routePattern: this.routeData.route,
cookies,
get clientAddress() {
return renderContext.clientAddress();
@ -581,33 +577,3 @@ export class RenderContext {
});
}
}
/**
* Return the component path without the `srcDir` and `pages`
* @param component
*/
function getAstroRoutePattern(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('/');
}

View file

@ -50,8 +50,8 @@ 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: \//);
assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \//);
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/);
@ -96,8 +96,8 @@ 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: \//);
assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \//);
html =await fixture.readFile('/omit-markdown-extensions/index.html');
$ = cheerio.load(html);
@ -139,8 +139,8 @@ 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: \//);
assert.match($("#pattern-middleware").text(), /Astro route pattern middleware: \//);
response = await app.render(new Request('https://example.com/omit-markdown-extensions'));
html = await response.text();
$ = cheerio.load(html);