summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Arsh <69170106+lilnasy@users.noreply.github.com> 2024-01-30 17:16:10 +0000
committerGravatar GitHub <noreply@github.com> 2024-01-30 22:46:10 +0530
commita40a0ff5883c7915dd55881dcebd052b9f94a0eb (patch)
treec6f2aa9d2413907963b804e0974a7f5c85e00761
parente9027f194b939ac5a4d795ee1a2c24e4a6fbefc0 (diff)
downloadastro-a40a0ff5883c7915dd55881dcebd052b9f94a0eb.tar.gz
astro-a40a0ff5883c7915dd55881dcebd052b9f94a0eb.tar.zst
astro-a40a0ff5883c7915dd55881dcebd052b9f94a0eb.zip
fix(ssg): consider trailingSlash for url (#9878)
* fix(ssg): consider trailingSlash for url * add changeset
-rw-r--r--.changeset/wet-rivers-do.md5
-rw-r--r--packages/astro/src/core/build/generate.ts7
-rw-r--r--packages/astro/test/astro-get-static-paths.test.js3
3 files changed, 12 insertions, 3 deletions
diff --git a/.changeset/wet-rivers-do.md b/.changeset/wet-rivers-do.md
new file mode 100644
index 000000000..a11d0f7bc
--- /dev/null
+++ b/.changeset/wet-rivers-do.md
@@ -0,0 +1,5 @@
+---
+"astro": patch
+---
+
+Fixes an issue where setting trailingSlash to "never" had no effect on `Astro.url`.
diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts
index 966d7ad84..e43f1ff5f 100644
--- a/packages/astro/src/core/build/generate.ts
+++ b/packages/astro/src/core/build/generate.ts
@@ -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
);
diff --git a/packages/astro/test/astro-get-static-paths.test.js b/packages/astro/test/astro-get-static-paths.test.js
index 66aa5b94d..3e35d6c85 100644
--- a/packages/astro/test/astro-get-static-paths.test.js
+++ b/packages/astro/test/astro-get-static-paths.test.js
@@ -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');
});
});