Skip to content

Commit

Permalink
change logic and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 16, 2024
1 parent 5197b30 commit a30529a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
37 changes: 35 additions & 2 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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,
Expand All @@ -59,6 +62,7 @@ export class RenderContext {
public props: Props = {},
) {
this.originalRoute = routeData;
this.astroRoute = getAstroRoute(routeData.component);
}

/**
Expand Down Expand Up @@ -241,6 +245,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);
}

Expand All @@ -257,7 +262,7 @@ export class RenderContext {

return {
cookies,
route: this.routeData.component,
route: this.astroRoute,
get clientAddress() {
return renderContext.clientAddress();
},
Expand Down Expand Up @@ -442,7 +447,7 @@ export class RenderContext {
return {
generator: astroStaticPartial.generator,
glob: astroStaticPartial.glob,
route: this.routeData.component,
route: this.astroRoute,
cookies,
get clientAddress() {
return renderContext.clientAddress();
Expand Down Expand Up @@ -574,3 +579,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("/");
}
15 changes: 9 additions & 6 deletions packages/astro/test/astro-global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
})
});

Expand Down Expand Up @@ -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]");
})
});

Expand Down Expand Up @@ -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/);
})
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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});
Expand All @@ -19,5 +21,6 @@ const canonicalURL = new URL(Astro.url.pathname, Astro.site ?? `http://example.c
<a class="post-url" href={data.url}>Read</a>
</div>
))}
<Route />
</body>
</html>

0 comments on commit a30529a

Please sign in to comment.