From 81e210e03c7d88c7b80b0b11a532c5b8e03cef93 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Tue, 12 Apr 2022 19:54:07 -0700 Subject: [PATCH] fix build base bug (#3068) * fix ssr url search params bug * fix build base bug * safer slash removal --- .changeset/nice-dingos-enjoy.md | 5 +++++ packages/astro/src/core/build/generate.ts | 4 ++-- packages/astro/src/core/path.ts | 13 +++++++++---- packages/astro/src/core/util.ts | 4 ++-- packages/astro/test/astro-global.test.js | 4 +--- 5 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 .changeset/nice-dingos-enjoy.md diff --git a/.changeset/nice-dingos-enjoy.md b/.changeset/nice-dingos-enjoy.md new file mode 100644 index 000000000..922b69104 --- /dev/null +++ b/.changeset/nice-dingos-enjoy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix an issue around build not respecting your base config diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index b1c03465b..17b419bb4 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -11,7 +11,7 @@ import type { } from '../../@types/astro'; import type { BuildInternals } from '../../core/build/internal.js'; import { debug, info } from '../logger/core.js'; -import { prependForwardSlash } from '../../core/path.js'; +import { prependForwardSlash, removeLeadingForwardSlash } from '../../core/path.js'; import type { RenderOptions } from '../../core/render/core'; import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js'; import { call as callEndpoint } from '../endpoint/index.js'; @@ -191,7 +191,7 @@ async function generatePath( } const ssr = isBuildingToSSR(opts.astroConfig); - const url = new URL(origin + pathname); + const url = new URL(opts.astroConfig.base + removeLeadingForwardSlash(pathname), origin); const options: RenderOptions = { legacyBuild: false, links, diff --git a/packages/astro/src/core/path.ts b/packages/astro/src/core/path.ts index 16945d960..a93b4e8e1 100644 --- a/packages/astro/src/core/path.ts +++ b/packages/astro/src/core/path.ts @@ -6,10 +6,18 @@ export function prependForwardSlash(path: string) { return path[0] === '/' ? path : '/' + path; } -export function removeEndingForwardSlash(path: string) { +export function removeTrailingForwardSlash(path: string) { return path.endsWith('/') ? path.slice(0, path.length - 1) : path; } +export function removeLeadingForwardSlash(path: string) { + return path.startsWith('/') ? path.substring(1) : path; +} + +export function trimSlashes(path: string) { + return path.replace(/^\/|\/$/g, ''); +} + export function startsWithForwardSlash(path: string) { return path[0] === '/'; } @@ -39,6 +47,3 @@ export function prependDotSlash(path: string) { return './' + path; } -export function trimSlashes(path: string) { - return path.replace(/^\/|\/$/g, ''); -} diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index 703e01ec9..8def24771 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -6,7 +6,7 @@ import slash from 'slash'; import { fileURLToPath, pathToFileURL } from 'url'; import type { ErrorPayload } from 'vite'; import type { AstroConfig } from '../@types/astro'; -import { removeEndingForwardSlash } from './path.js'; +import { removeTrailingForwardSlash } from './path.js'; /** Returns true if argument is an object of any prototype/class (but not null). */ export function isObject(value: unknown): value is Record { @@ -37,7 +37,7 @@ export function getOutputFilename(astroConfig: AstroConfig, name: string) { if (astroConfig.build.format === 'directory' && !STATUS_CODE_REGEXP.test(name)) { return path.posix.join(name, 'index.html'); } - return `${removeEndingForwardSlash(name || 'index')}.html`; + return `${removeTrailingForwardSlash(name || 'index')}.html`; } /** is a specifier an npm package? */ diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index cf1dabfc8..46564ed0d 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -38,9 +38,7 @@ describe('Astro.*', () => { await fixture.build(); }); - // BUG: Doesn't seem like `base` config is being respected in build, - // most values are incorrect actual, does not match expected. - it.skip('Astro.request.url', async () => { + it('Astro.request.url', async () => { const html = await fixture.readFile('/index.html'); const $ = cheerio.load(html);