Skip to content

Commit

Permalink
fix(ssg): consider trailingSlash for url (#9878)
Browse files Browse the repository at this point in the history
* fix(ssg): consider trailingSlash for url

* add changeset
  • Loading branch information
lilnasy authored Jan 30, 2024
1 parent e9027f1 commit a40a0ff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-rivers-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where setting trailingSlash to "never" had no effect on `Astro.url`.
7 changes: 5 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url';
import PQueue from 'p-queue';
import type { OutputAsset, OutputChunk } from 'rollup';
import type {
AstroConfig,
AstroSettings,
ComponentInstance,
GetStaticPathsItem,
Expand Down Expand Up @@ -455,15 +456,16 @@ function getUrlForPath(
pathname: string,
base: string,
origin: string,
format: 'directory' | 'file',
format: AstroConfig["build"]["format"],
trailingSlash: AstroConfig["trailingSlash"],
routeType: RouteType
): URL {
/**
* Examples:
* pathname: /, /foo
* base: /
*/
const ending = format === 'directory' ? '/' : '.html';
const ending = format === 'directory' ? trailingSlash === 'never' ? '' : '/' : '.html';
let buildPathname: string;
if (pathname === '/' || pathname === '') {
buildPathname = base;
Expand Down Expand Up @@ -538,6 +540,7 @@ async function generatePath(
pipeline.getConfig().base,
pipeline.getStaticBuildOptions().origin,
pipeline.getConfig().build.format,
pipeline.getConfig().trailingSlash,
route.type
);

Expand Down
3 changes: 2 additions & 1 deletion packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('getStaticPaths - build calls', () => {
fixture = await loadFixture({
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
trailingSlash: 'never',
base: '/blog',
});
await fixture.build();
Expand All @@ -29,7 +30,7 @@ describe('getStaticPaths - build calls', () => {
const html = await fixture.readFile('/food/tacos/index.html');
const $ = cheerio.load(html);

expect($('#url').text()).to.equal('/blog/food/tacos/');
expect($('#url').text()).to.equal('/blog/food/tacos');
});
});

Expand Down

0 comments on commit a40a0ff

Please sign in to comment.