fix build base bug (#3068)

* fix ssr url search params bug

* fix build base bug

* safer slash removal
This commit is contained in:
Fred K. Schott 2022-04-12 19:54:07 -07:00 committed by GitHub
parent 564caf24c2
commit 81e210e03c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 11 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix an issue around build not respecting your base config

View file

@ -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,

View file

@ -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, '');
}

View file

@ -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<string, any> {
@ -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? */

View file

@ -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);