diff options
Diffstat (limited to '')
-rw-r--r-- | .changeset/six-grapes-look.md | 15 | ||||
-rw-r--r-- | packages/astro/e2e/astro-envs.test.js | 4 | ||||
-rw-r--r-- | packages/astro/src/@types/astro.ts | 7 | ||||
-rw-r--r-- | packages/astro/src/core/build/generate.ts | 5 | ||||
-rw-r--r-- | packages/astro/src/core/config/schema.ts | 22 | ||||
-rw-r--r-- | packages/astro/test/astro-envs.test.js | 4 | ||||
-rw-r--r-- | packages/astro/test/astro-global.test.js | 6 | ||||
-rw-r--r-- | packages/astro/test/dev-routing.test.js | 8 | ||||
-rw-r--r-- | packages/astro/test/preview-routing.test.js | 8 | ||||
-rw-r--r-- | packages/astro/test/public-base-404.test.js | 2 |
10 files changed, 42 insertions, 39 deletions
diff --git a/.changeset/six-grapes-look.md b/.changeset/six-grapes-look.md new file mode 100644 index 000000000..edf10e01a --- /dev/null +++ b/.changeset/six-grapes-look.md @@ -0,0 +1,15 @@ +--- +'astro': major +--- + +The value of `import.meta.env.BASE_URL`, which is derived from the `base` option, will no longer have a trailing slash added by default or when `trailingSlash: "ignore"` is set. The existing behavior of `base` in combination with `trailingSlash: "always"` or `trailingSlash: "never"` is unchanged. + +If your `base` already has a trailing slash, no change is needed. + +If your `base` does not have a trailing slash, add one to preserve the previous behaviour: + +```diff +// astro.config.mjs +- base: 'my-base', ++ base: 'my-base/', +``` diff --git a/packages/astro/e2e/astro-envs.test.js b/packages/astro/e2e/astro-envs.test.js index 1a4f4a1ca..50cff6e89 100644 --- a/packages/astro/e2e/astro-envs.test.js +++ b/packages/astro/e2e/astro-envs.test.js @@ -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' ); }); }); diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index dbebe4adf..4491dc48c 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -572,12 +572,7 @@ export interface AstroUserConfig { * * When using this option, all of your static asset imports and URLs should add the base as a prefix. You can access this value via `import.meta.env.BASE_URL`. * - * By default, the value of `import.meta.env.BASE_URL` includes a trailing slash. If you have the [`trailingSlash`](https://docs.astro.build/en/reference/configuration-reference/#trailingslash) option set to `'never'`, you will need to add it manually in your static asset imports and URLs. - * - * ```astro - * <a href="/docs/about/">About</a> - * <img src=`${import.meta.env.BASE_URL}image.png`> - * ``` + * The value of `import.meta.env.BASE_URL` respects your `trailingSlash` config and will include a trailing slash if you explicitly include one or if `trailingSlash: "always"` is set. If `trailingSlash: "never"` is set, `BASE_URL` will not include a trailing slash, even if `base` includes one. */ base?: string; diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 39f30f744..3e3a44ce0 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -28,6 +28,7 @@ import { } from '../../core/build/internal.js'; import { isRelativePath, + joinPaths, prependForwardSlash, removeLeadingForwardSlash, removeTrailingForwardSlash, @@ -437,11 +438,11 @@ function getUrlForPath( buildPathname = base; } else if (routeType === 'endpoint') { const buildPathRelative = removeLeadingForwardSlash(pathname); - buildPathname = base + buildPathRelative; + buildPathname = joinPaths(base, buildPathRelative); } else { const buildPathRelative = removeTrailingForwardSlash(removeLeadingForwardSlash(pathname)) + ending; - buildPathname = base + buildPathRelative; + buildPathname = joinPaths(base, buildPathRelative); } const url = new URL(buildPathname, origin); return url; diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 417f918bb..64dda0f93 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -8,7 +8,7 @@ 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 } from '../path.js'; const ASTRO_CONFIG_DEFAULTS = { root: '.', @@ -366,22 +366,14 @@ 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 !== '/') { - 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); + // Handle `base` trailing slash based on `trailingSlash` config + 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; diff --git a/packages/astro/test/astro-envs.test.js b/packages/astro/test/astro-envs.test.js index 402878da5..c923ae065 100644 --- a/packages/astro/test/astro-envs.test.js +++ b/packages/astro/test/astro-envs.test.js @@ -109,7 +109,7 @@ 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 () => { @@ -117,7 +117,7 @@ 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'); }); }); }); diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index d49868584..f003bc035 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -54,10 +54,10 @@ describe('Astro Global', () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html); - expect($('#pathname').text()).to.equal('/blog/'); + expect($('#pathname').text()).to.equal('/blog'); expect($('#searchparams').text()).to.equal('{}'); - expect($('#child-pathname').text()).to.equal('/blog/'); - expect($('#nested-child-pathname').text()).to.equal('/blog/'); + expect($('#child-pathname').text()).to.equal('/blog'); + expect($('#nested-child-pathname').text()).to.equal('/blog'); }); it('Astro.site', async () => { diff --git a/packages/astro/test/dev-routing.test.js b/packages/astro/test/dev-routing.test.js index 186355b43..ff5f3a75d 100644 --- a/packages/astro/test/dev-routing.test.js +++ b/packages/astro/test/dev-routing.test.js @@ -113,9 +113,9 @@ describe('Development Routing', () => { expect(response.status).to.equal(200); }); - it('404 when loading subpath root without trailing slash', async () => { + it('200 when loading subpath root without trailing slash', async () => { const response = await fixture.fetch('/blog'); - expect(response.status).to.equal(404); + expect(response.status).to.equal(200); }); it('200 when loading another page with subpath used', async () => { @@ -163,9 +163,9 @@ describe('Development Routing', () => { expect(response.status).to.equal(200); }); - it('404 when loading subpath root without trailing slash', async () => { + it('200 when loading subpath root without trailing slash', async () => { const response = await fixture.fetch('/blog'); - expect(response.status).to.equal(404); + expect(response.status).to.equal(200); }); it('200 when loading another page with subpath used', async () => { diff --git a/packages/astro/test/preview-routing.test.js b/packages/astro/test/preview-routing.test.js index 5c365b07c..4c99881b6 100644 --- a/packages/astro/test/preview-routing.test.js +++ b/packages/astro/test/preview-routing.test.js @@ -157,9 +157,9 @@ describe('Preview Routing', () => { expect(response.status).to.equal(200); }); - it('404 when loading subpath root without trailing slash', async () => { + it('200 when loading subpath root without trailing slash', async () => { const response = await fixture.fetch('/blog'); - expect(response.status).to.equal(404); + expect(response.status).to.equal(200); }); it('200 when loading another page with subpath used', async () => { @@ -345,9 +345,9 @@ describe('Preview Routing', () => { expect(response.status).to.equal(200); }); - it('404 when loading subpath root without trailing slash', async () => { + it('200 when loading subpath root without trailing slash', async () => { const response = await fixture.fetch('/blog'); - expect(response.status).to.equal(404); + expect(response.status).to.equal(200); }); it('200 when loading another page with subpath used', async () => { diff --git a/packages/astro/test/public-base-404.test.js b/packages/astro/test/public-base-404.test.js index c8d58471d..969e2a952 100644 --- a/packages/astro/test/public-base-404.test.js +++ b/packages/astro/test/public-base-404.test.js @@ -44,7 +44,7 @@ describe('Public dev with base', () => { expect(response.status).to.equal(404); const html = await response.text(); $ = cheerio.load(html); - expect($('a').first().text()).to.equal('/blog/'); + expect($('a').first().text()).to.equal('/blog'); }); it('default 404 page when loading /none/', async () => { |