Skip to content

Commit

Permalink
Preserve base slash with trailingSlash ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Aug 1, 2023
1 parent 77ab4bb commit 8441295
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-grapes-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': major
---

Preserve base slash with `trailingSlash: "ignore"`
4 changes: 2 additions & 2 deletions packages/astro/e2e/astro-envs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ test.describe('Astro Environment BASE_URL', () => {
await page.goto(astro.resolveUrl('/blog/'));

const astroBaseUrl = page.locator('id=astro-base-url');
await expect(astroBaseUrl, 'astroBaseUrl equals to /blog/').toHaveText('/blog/');
await expect(astroBaseUrl, 'astroBaseUrl equals to /blog').toHaveText('/blog');

const clientComponentBaseUrl = page.locator('id=client-component-base-url');
await expect(clientComponentBaseUrl, 'clientComponentBaseUrl equals to /blog').toHaveText(
'/blog/'
'/blog'
);
});
});
18 changes: 12 additions & 6 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { BUNDLED_THEMES } from 'shiki';
import { z } from 'zod';
import { appendForwardSlash, prependForwardSlash, trimSlashes } from '../path.js';
import {
appendForwardSlash,
prependForwardSlash,
removeTrailingForwardSlash,
trimSlashes,
} from '../path.js';

const ASTRO_CONFIG_DEFAULTS = {
root: '.',
Expand Down Expand Up @@ -366,22 +371,23 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
) {
config.build.client = new URL('./dist/client/', config.outDir);
}
const trimmedBase = trimSlashes(config.base);

// If there is no base but there is a base for site config, warn.
const sitePathname = config.site && new URL(config.site).pathname;
if (!trimmedBase.length && sitePathname && sitePathname !== '/') {
if (!trimSlashes(config.base).length && sitePathname && sitePathname !== '/') {
config.base = sitePathname;
/* eslint-disable no-console */
console.warn(`The site configuration value includes a pathname of ${sitePathname} but there is no base configuration.
A future version of Astro will stop using the site pathname when producing <link> and <script> tags. Set your site's base with the base configuration.`);
}

if (trimmedBase.length && config.trailingSlash === 'never') {
config.base = prependForwardSlash(trimmedBase);
if (config.trailingSlash === 'never') {
config.base = prependForwardSlash(removeTrailingForwardSlash(config.base));
} else if (config.trailingSlash === 'always') {
config.base = prependForwardSlash(appendForwardSlash(config.base));
} else {
config.base = prependForwardSlash(appendForwardSlash(trimmedBase));
config.base = prependForwardSlash(config.base);
}

return config;
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/astro-envs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ describe('Environment Variables', () => {
expect(res.status).to.equal(200);
let indexHtml = await res.text();
let $ = cheerio.load(indexHtml);
expect($('#base-url').text()).to.equal('/blog/');
expect($('#base-url').text()).to.equal('/blog');
});

it('does render destructured builtin SITE env', async () => {
let res = await fixture.fetch('/blog/destructured/');
expect(res.status).to.equal(200);
let indexHtml = await res.text();
let $ = cheerio.load(indexHtml);
expect($('#base-url').text()).to.equal('/blog/');
expect($('#base-url').text()).to.equal('/blog');
});
});
});

0 comments on commit 8441295

Please sign in to comment.