0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

fix(ssg): consider trailingSlash for url (#9878)

* fix(ssg): consider trailingSlash for url

* add changeset
This commit is contained in:
Arsh 2024-01-30 17:16:10 +00:00 committed by GitHub
parent e9027f194b
commit a40a0ff588
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Fixes an issue where setting trailingSlash to "never" had no effect on `Astro.url`.

View file

@ -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,
@ -455,7 +456,8 @@ function getUrlForPath(
pathname: string,
base: string,
origin: string,
format: 'directory' | 'file',
format: AstroConfig["build"]["format"],
trailingSlash: AstroConfig["trailingSlash"],
routeType: RouteType
): URL {
/**
@ -463,7 +465,7 @@ function getUrlForPath(
* pathname: /, /foo
* base: /
*/
const ending = format === 'directory' ? '/' : '.html';
const ending = format === 'directory' ? trailingSlash === 'never' ? '' : '/' : '.html';
let buildPathname: string;
if (pathname === '/' || pathname === '') {
buildPathname = base;
@ -538,6 +540,7 @@ async function generatePath(
pipeline.getConfig().base,
pipeline.getStaticBuildOptions().origin,
pipeline.getConfig().build.format,
pipeline.getConfig().trailingSlash,
route.type
);

View file

@ -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();
@ -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');
});
});