Skip to content

Commit

Permalink
fix(qwik-city): enforce / on base paths (#6301)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens authored May 15, 2024
1 parent 7bd3bcc commit b381b22
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
22 changes: 17 additions & 5 deletions packages/qwik-city/buildtime/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ function normalizeOptions(
viteBasePath: string,
userOpts: PluginOptions | undefined
) {
if (!(viteBasePath.startsWith('/') && viteBasePath.endsWith('/'))) {
// TODO v2: make this an error
console.error(
`warning: vite's config.base must begin and end with /. This will be an error in v2. If you have a valid use case, please open an issue.`
);
if (!viteBasePath.endsWith('/')) {
viteBasePath += '/';
}
}
const opts: NormalizedPluginOptions = { ...userOpts } as any;

if (typeof opts.routesDir !== 'string') {
Expand All @@ -71,15 +80,18 @@ function normalizeOptions(
// but in most cases should be passed in by the vite config "base" property
opts.basePathname = viteBasePath;
}

// cleanup basePathname
const url = new URL(opts.basePathname, 'https://qwik.builer.io/');
opts.basePathname = url.pathname;
if (!opts.basePathname.endsWith('/')) {
// basePathname should always start and end with a slash
// TODO v2: make this an error
console.error(
`Warning: qwik-city plugin basePathname must end with /. This will be an error in v2`
);
opts.basePathname += '/';
}

// cleanup basePathname
const url = new URL(opts.basePathname, 'https://qwik.dev/');
opts.basePathname = url.pathname;

if (typeof opts.trailingSlash !== 'boolean') {
opts.trailingSlash = true;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/qwik/src/core/components/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const PrefetchServiceWorker = (opts: {
fetchBundleGraph?: boolean;
}) => {
const resolvedOpts = {
base: '/',
base: import.meta.env.BASE_URL,
verbose: false,
path: 'qwik-prefetch-service-worker.js',
...opts,
Expand Down Expand Up @@ -96,7 +96,7 @@ export const PrefetchGraph = (
opts: { base?: string; manifestHash?: string; manifestURL?: string } = {}
) => {
const resolvedOpts = {
base: '/build/',
base: `${import.meta.env.BASE_URL}build/`,
manifestHash: null,
manifestURL: null,
...opts,
Expand Down
9 changes: 9 additions & 0 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,15 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {

async configResolved(config) {
basePathname = config.base;
if (!(basePathname.startsWith('/') && basePathname.endsWith('/'))) {
// TODO v2: make this an error
console.error(
`warning: vite's config.base must begin and end with /. This will be an error in v2. If you have a valid use case, please open an issue.`
);
if (!basePathname.endsWith('/')) {
basePathname += '/';
}
}
const sys = qwikPlugin.getSys();
if (sys.env === 'node' && !qwikViteOpts.entryStrategy) {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik/src/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getBuildBase(opts: RenderToStringOptions) {
}
return base;
}
return '/build/';
return `${import.meta.env.BASE_URL}build/`;
}

/** @public */
Expand Down
4 changes: 2 additions & 2 deletions starters/features/localize/src/routes/[locale]/i18n-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ export function extractLang(locale: string): string {
*/
export function extractBase({ serverData }: RenderOptions): string {
if (import.meta.env.DEV) {
return "/build";
return `${import.meta.env.BASE_URL}build`;
} else {
return "/build/" + serverData!.locale;
return `${import.meta.env.BASE_URL}build/` + serverData!.locale;
}
}

Expand Down

0 comments on commit b381b22

Please sign in to comment.