Skip to content

Commit 372c9da

Browse files
committed
feat(router): refactor dev server, allow routes hot reload
- remove separate dev-server which duplicated route processing logic, instead use node adapter - watch routes for changes and reload when needed - make route imports always dynamic (optionally static). This limits the amount of code to load when not all paths are visited, and reduces startup time. - remove .testing condition on double import, the reason was the dev-server importing core separately from the test code Still missing: - transformindexhtml handling - entry.ts support - serviceworker support
1 parent 14f7132 commit 372c9da

File tree

13 files changed

+186
-622
lines changed

13 files changed

+186
-622
lines changed

e2e/qwik-cli-e2e/tests/serve.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ for (const type of ['empty', 'playground'] as QwikProjectType[]) {
3333
}
3434
config.cleanupFn();
3535
};
36-
});
36+
}, 120000);
3737

3838
if (type === 'playground') {
3939
test(

packages/qwik-router/src/buildtime/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export function createBuildContext(
66
rootDir: string,
77
viteBasePath: string,
88
userOpts?: PluginOptions,
9-
target?: 'ssr' | 'client'
9+
target?: 'ssr' | 'client',
10+
dynamicImports?: boolean
1011
) {
1112
const ctx: BuildContext = {
1213
rootDir: normalizePath(rootDir),
@@ -20,8 +21,7 @@ export function createBuildContext(
2021
diagnostics: [],
2122
frontmatter: new Map(),
2223
target: target || 'ssr',
23-
isDevServer: false,
24-
isDevServerClientOnly: false,
24+
dynamicImports: target === 'client' || !!dynamicImports,
2525
isDirty: true,
2626
activeBuild: null,
2727
};

packages/qwik-router/src/buildtime/runtime-generation/generate-qwik-router-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function generateQwikRouterConfig(
1515
const c: string[] = [];
1616

1717
c.push(`\n/** Qwik Router Config */`);
18+
c.push(`\nimport { isDev } from '@qwik.dev/core/build';`);
1819

1920
createServerPlugins(ctx, qwikPlugin, c, esmImports, isSSR);
2021

@@ -28,11 +29,10 @@ export function generateQwikRouterConfig(
2829

2930
c.push(`export const basePathname = ${JSON.stringify(ctx.opts.basePathname)};`);
3031

31-
c.push(`export const cacheModules = ${JSON.stringify(!ctx.isDevServer)};`);
32+
c.push(`export const cacheModules = !isDev;`);
3233

3334
c.push(
3435
`export default { routes, serverPlugins, menus, trailingSlash, basePathname, cacheModules };`
3536
);
36-
3737
return esmImports.join('\n') + c.join('\n');
3838
}

packages/qwik-router/src/buildtime/runtime-generation/generate-routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function createRoutes(
1111
isSSR: boolean
1212
) {
1313
const includeEndpoints = isSSR;
14-
const dynamicImports = ctx.target === 'client';
14+
const dynamicImports = ctx.dynamicImports;
1515

1616
if (ctx.layouts.length > 0) {
1717
c.push(`\n/** Qwik Router Layouts (${ctx.layouts.length}) */`);

packages/qwik-router/src/buildtime/runtime-generation/generate-service-worker.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,17 @@ import type { BuildContext } from '../types';
33
export function generateServiceWorkerRegister(ctx: BuildContext, swRegister: string) {
44
let swReg: string;
55

6-
if (ctx.isDevServer) {
7-
swReg = SW_UNREGISTER;
8-
} else {
9-
swReg = swRegister;
6+
swReg = swRegister;
107

11-
let swUrl = '/service-worker.js';
12-
if (ctx.serviceWorkers.length > 0) {
13-
const sw = ctx.serviceWorkers.sort((a, b) =>
14-
a.chunkFileName.length < b.chunkFileName.length ? -1 : 1
15-
)[0];
16-
swUrl = ctx.opts.basePathname + sw.chunkFileName;
17-
}
18-
19-
swReg = swReg.replace('__url', swUrl);
8+
let swUrl = '/service-worker.js';
9+
if (ctx.serviceWorkers.length > 0) {
10+
const sw = ctx.serviceWorkers.sort((a, b) =>
11+
a.chunkFileName.length < b.chunkFileName.length ? -1 : 1
12+
)[0];
13+
swUrl = ctx.opts.basePathname + sw.chunkFileName;
2014
}
2115

16+
swReg = swReg.replace('__url', swUrl);
17+
2218
return `export default ${JSON.stringify(swReg)};`;
2319
}
24-
25-
const SW_UNREGISTER = `
26-
navigator.serviceWorker?.getRegistrations().then((regs) => {
27-
for (const reg of regs) {
28-
reg.unregister();
29-
}
30-
});
31-
`;

packages/qwik-router/src/buildtime/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ export interface BuildContext {
1212
frontmatter: Map<string, FrontmatterAttrs>;
1313
diagnostics: Diagnostic[];
1414
target: 'ssr' | 'client' | undefined;
15-
isDevServer: boolean;
16-
isDevServerClientOnly: boolean;
15+
dynamicImports: boolean;
1716
isDirty: boolean;
1817
activeBuild: Promise<void> | null;
1918
}

0 commit comments

Comments
 (0)