diff --git a/.changeset/six-grapes-look.md b/.changeset/six-grapes-look.md new file mode 100644 index 000000000000..edf10e01aa25 --- /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 1a4f4a1ca7aa..50cff6e89557 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 dbebe4adf436..4491dc48ccd4 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 - * About - * - * ``` + * 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 39f30f744aee..3e3a44ce021a 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 417f918bb6b8..64dda0f936c5 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 and