Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(qwik-city): support vite.base in dev mode #6017

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/qwik-city/runtime/src/utils.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ test(`isSameOrigin`, () => {
);
});
});

test('missing clientNavPath', () => {
const clientNavPath = null;
const currentLoc = new URL('https://qwik.dev/contact');
Expand Down
5 changes: 3 additions & 2 deletions packages/qwik/src/core/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ export const createPlatform = (): CorePlatform => {
* @returns Fully qualified URL.
*/
export const toUrl = (doc: Document, containerEl: QwikElement, url: string | URL): URL => {
const baseURI = doc.baseURI;
const baseURI = new URL(doc.baseURI);
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
const base = new URL(containerEl.getAttribute('q:base') ?? baseURI, baseURI);
return new URL(url, base);
const pathUrl = (base.pathname + url).replace(/\/+/g, '/');
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
return new URL(pathUrl, base.origin);
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
};

let _platform = /*#__PURE__ */ createPlatform();
Expand Down
9 changes: 9 additions & 0 deletions packages/qwik/src/optimizer/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {
},
inlineStylesUpToBytes: null as any,
lint: true,
base: '',
};

const init = async () => {
Expand Down Expand Up @@ -134,6 +135,13 @@ export function createPlugin(optimizerOptions: OptimizerOptions = {}) {

opts.debug = !!updatedOpts.debug;

opts.base =
!updatedOpts?.base || updatedOpts.base === '/'
? ''
: updatedOpts.base.endsWith('/')
? updatedOpts.base.slice(0, -1)
: updatedOpts.base;

updatedOpts.target === 'test';
if (
updatedOpts.target === 'ssr' ||
Expand Down Expand Up @@ -957,6 +965,7 @@ export interface QwikPluginOptions {
* large projects. Defaults to `true`
*/
lint?: boolean;
base?: string;
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
}

export interface NormalizedQwikPluginOptions extends Required<QwikPluginOptions> {
Expand Down
7 changes: 4 additions & 3 deletions packages/qwik/src/optimizer/src/plugins/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export async function configureDevServer(
location: 'head',
attributes: {
rel: 'stylesheet',
href: url,
href: `${opts.base}${url}`,
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
},
});
}
Expand All @@ -135,6 +135,7 @@ export async function configureDevServer(
: 'src';

const renderOpts: RenderToStreamOptions = {
base: opts.base,
debug: true,
locale: serverData.locale,
stream: res,
Expand Down Expand Up @@ -183,7 +184,7 @@ export async function configureDevServer(
pathId.endsWith(ext)
)
) {
res.write(`<link rel="stylesheet" href="${v.url}">`);
res.write(`<link rel="stylesheet" href="${opts.base}${v.url}">`);
}
});
});
Expand Down Expand Up @@ -374,7 +375,7 @@ const DEV_QWIK_INSPECTOR = (opts: NormalizedQwikPluginOptions['devTools'], srcDi

const END_SSR_SCRIPT = (opts: NormalizedQwikPluginOptions, srcDir: string) => `
<style>${VITE_ERROR_OVERLAY_STYLES}</style>
<script type="module" src="/@vite/client"></script>
<script type="module" src="${opts.base}/@vite/client"></script>
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
${errorHost}
${perfWarning}
${DEV_QWIK_INSPECTOR(opts.devTools, srcDir)}
Expand Down
1 change: 1 addition & 0 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any {
devTools: qwikViteOpts.devTools,
sourcemap: !!viteConfig.build?.sourcemap,
lint: qwikViteOpts.lint,
base: viteConfig.base,
JerryWu1234 marked this conversation as resolved.
Show resolved Hide resolved
};
if (!qwikViteOpts.csr) {
if (target === 'ssr') {
Expand Down
6 changes: 4 additions & 2 deletions packages/qwik/src/qwikloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ export const qwikLoader = (doc: Document, hasInitialized?: number) => {
const attrValue = element[getAttribute](attrName);
if (attrValue) {
const container = element.closest('[q\\:container]')!;
const base = new URL(container[getAttribute]('q:base')!, doc.baseURI);
const baseURI = new URL(doc.baseURI);
const base = new URL(container.getAttribute('q:base') ?? baseURI, baseURI);
for (const qrl of attrValue.split('\n')) {
const url = new URL(qrl, base);
const pathUrl = (base.pathname + qrl).replace(/\/+/g, '/');
const url = new URL(pathUrl, base.origin);
const symbolName = url.hash[replace](/^#?([^?[|]*).*$/, '$1') || 'default';
const reqTime = performance.now();
let handler: any;
Expand Down
6 changes: 4 additions & 2 deletions packages/qwik/src/testing/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ export function setTestPlatform(_setPlatform: Function) {
* @returns Fully qualified URL.
*/
export function toUrl(doc: Document, containerEl: Element, url: string | URL): URL {
const base = new URL(containerEl?.getAttribute('q:base') ?? doc.baseURI, doc.baseURI);
return new URL(url, base);
const baseURI = new URL(doc.baseURI);
const base = new URL(containerEl.getAttribute('q:base') ?? baseURI, baseURI);
const pathUrl = (base.pathname + url).replace(/\/+/g, '/');
return new URL(pathUrl, base.origin);
}

function toPath(url: URL) {
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion starters/apps/basic/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RouterHead } from "./components/router-head/router-head";

import "./global.css";

const BASE_URL = import.meta.env.BASE_URL;
export default component$(() => {
/**
* The root of a QwikCity site always start with the <QwikCityProvider> component,
Expand All @@ -20,7 +21,7 @@ export default component$(() => {
<QwikCityProvider>
<head>
<meta charSet="utf-8" />
<link rel="manifest" href="/manifest.json" />
<link rel="manifest" href={`${BASE_URL}manifest.json`} />
PatrickJS marked this conversation as resolved.
Show resolved Hide resolved
<RouterHead />
<ServiceWorkerRegister />
</head>
Expand Down
4 changes: 2 additions & 2 deletions starters/apps/empty/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { RouterHead } from "./components/router-head/router-head";

import "./global.css";

const BASE_URL = import.meta.env.BASE_URL;
export default component$(() => {
/**
* The root of a QwikCity site always start with the <QwikCityProvider> component,
Expand All @@ -20,7 +20,7 @@ export default component$(() => {
<QwikCityProvider>
<head>
<meta charSet="utf-8" />
<link rel="manifest" href="/manifest.json" />
<link rel="manifest" href={`${BASE_URL}manifest.json`} />
<RouterHead />
</head>
<body lang="en">
Expand Down
3 changes: 2 additions & 1 deletion starters/apps/site-with-visual-cms/src/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RouterHead } from "./components/router-head/router-head";

import "./global.css";

const BASE_URL = import.meta.env.BASE_URL;
export default component$(() => {
/**
* The root of a QwikCity site always start with the <QwikCityProvider> component,
Expand All @@ -20,7 +21,7 @@ export default component$(() => {
<QwikCityProvider>
<head>
<meta charSet="utf-8" />
<link rel="manifest" href="/manifest.json" />
<link rel="manifest" href={`${BASE_URL}manifest.json`} />
<RouterHead />
<ServiceWorkerRegister />
</head>
Expand Down
Loading