Skip to content

Commit 09c231b

Browse files
committed
fix(router): better embed statics/404s
1 parent f328e70 commit 09c231b

File tree

25 files changed

+244
-379
lines changed

25 files changed

+244
-379
lines changed

packages/qwik-router/global.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ declare var __DEFAULT_LOADERS_SERIALIZATION_STRATEGY__: SerializationStrategy;
2424

2525
/** Should routes not have a trailing slash? */
2626
declare var __NO_TRAILING_SLASH__: boolean;
27+
28+
declare var __QWIK_BUILD_DIR__: string;
29+
declare var __QWIK_ASSETS_DIR__: string;

packages/qwik-router/middleware/request-handler/generated/not-found-paths.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/qwik-router/middleware/request-handler/generated/static-paths.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

packages/qwik-router/modules.d.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,12 @@ declare module '@qwik-city-plan' {
1919
export { default } from '@qwik-router-config';
2020
}
2121

22-
declare module '@qwik-router-not-found-paths' {
23-
function getNotFound(_pathname: string): string;
24-
export { getNotFound };
25-
}
26-
2722
declare module '@qwik-city-not-found-paths' {
28-
export * from '@qwik-router-not-found-paths';
29-
}
30-
31-
declare module '@qwik-router-static-paths' {
32-
function isStaticPath(method: string, url: URL): boolean;
33-
export { isStaticPath };
23+
/** @deprecated Use `getNotFound` from `@qwik.dev/router/middleware/request-handler` instead. */
24+
export { getNotFound } from '@qwik.dev/router/middleware/request-handler';
3425
}
3526

3627
declare module '@qwik-city-static-paths' {
37-
export * from '@qwik-router-static-paths';
28+
/** @deprecated Use `isStaticPath` from `@qwik.dev/router/middleware/request-handler` instead. */
29+
export { isStaticPath } from '@qwik.dev/router/middleware/request-handler';
3830
}

packages/qwik-router/src/adapters/shared/adapter.shared.api.md

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,14 @@ export interface AdapterSSGOptions extends Omit<SsgRenderOptions, 'outDir' | 'or
1818
// @public (undocumented)
1919
export function getParentDir(startDir: string, dirName: string): string;
2020

21-
// @public (undocumented)
22-
export const NOT_FOUND_PATHS_ID = "@qwik-router-not-found-paths";
23-
24-
// @public (undocumented)
25-
export const RESOLVED_NOT_FOUND_PATHS_ID = "@qwik-router-not-found-paths.js";
26-
27-
// @public (undocumented)
28-
export const RESOLVED_STATIC_PATHS_ID = "@qwik-router-static-paths.js";
29-
3021
// @public (undocumented)
3122
export interface ServerAdapterOptions {
3223
ssg?: AdapterSSGOptions | null;
3324
}
3425

35-
// @public (undocumented)
36-
export const STATIC_PATHS_ID = "@qwik-router-static-paths";
37-
3826
// Warning: (ae-forgotten-export) The symbol "ViteAdapterPluginOptions" needs to be exported by the entry point index.d.ts
3927
//
40-
// @public (undocumented)
28+
// @public
4129
export function viteAdapter(opts: ViteAdapterPluginOptions): Plugin_2<never>;
4230

4331
// (No @packageDocumentation comment for this package)

packages/qwik-router/src/adapters/shared/vite/index.ts

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import type { QwikVitePlugin } from '@qwik.dev/core/optimizer';
22
import type { StaticGenerateOptions, SsgRenderOptions } from 'packages/qwik-router/src/ssg';
33
import type { QwikRouterPlugin } from '@qwik.dev/router/vite';
4-
import fs from 'node:fs';
54
import { basename, dirname, join, resolve } from 'node:path';
65
import type { Plugin, UserConfig } from 'vite';
76
import type { BuildRoute } from '../../../buildtime/types';
87
import { postBuild } from './post-build';
98

10-
/** @public */
9+
/**
10+
* Implements the SSG after the build is complete. Also provides a `generate(...)` callback that is
11+
* called after the SSG is complete, which allows for custom post-processing of the complete build
12+
* results.
13+
*
14+
* @public
15+
*/
1116
export function viteAdapter(opts: ViteAdapterPluginOptions) {
1217
let qwikRouterPlugin: QwikRouterPlugin | null = null;
1318
let qwikVitePlugin: QwikVitePlugin | null = null;
1419
let serverOutDir: string | null = null;
1520
let renderModulePath: string | null = null;
1621
let qwikRouterConfigModulePath: string | null = null;
1722
let isSsrBuild = false;
18-
let format = 'esm';
1923
const outputEntries: string[] = [];
2024

2125
const plugin: Plugin<never> = {
22-
name: `vite-plugin-qwik-router-${opts.name}`,
26+
name: `vite-plugin-qwik-router-ssg-${opts.name}`,
2327
enforce: 'post',
2428
apply: 'build',
2529

@@ -63,11 +67,6 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
6367
`"build.rollupOptions.input" must be set in order to use the "${opts.name}" adapter.`
6468
);
6569
}
66-
67-
// @ts-ignore `format` removed in Vite 5
68-
if (config.ssr?.format === 'cjs') {
69-
format = 'cjs';
70-
}
7170
}
7271
},
7372
buildStart() {
@@ -108,13 +107,7 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
108107
closeBundle: {
109108
sequential: true,
110109
async handler() {
111-
if (
112-
isSsrBuild &&
113-
opts.ssg !== null &&
114-
serverOutDir &&
115-
qwikRouterPlugin?.api &&
116-
qwikVitePlugin?.api
117-
) {
110+
if (isSsrBuild && serverOutDir && qwikRouterPlugin?.api && qwikVitePlugin?.api) {
118111
const staticPaths: string[] = opts.staticPaths || [];
119112
const routes = qwikRouterPlugin.api.getRoutes();
120113
const basePathname = qwikRouterPlugin.api.getBasePathname();
@@ -124,6 +117,7 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
124117

125118
const rootDir = qwikVitePlugin.api.getRootDir() ?? undefined;
126119
if (
120+
opts.ssg !== null &&
127121
renderModulePath &&
128122
qwikRouterConfigModulePath &&
129123
clientOutDir &&
@@ -171,42 +165,36 @@ export function viteAdapter(opts: ViteAdapterPluginOptions) {
171165
}
172166

173167
staticPaths.push(...staticGenerateResult.staticPaths);
168+
}
174169

175-
const { staticPathsCode, notFoundPathsCode } = await postBuild(
176-
clientPublicOutDir,
177-
assetsDir ? join(basePathname, assetsDir) : basePathname,
178-
staticPaths,
179-
format,
180-
!!opts.cleanStaticGenerated
181-
);
170+
await postBuild(
171+
clientPublicOutDir,
172+
serverOutDir,
173+
assetsDir ? join(basePathname, assetsDir) : basePathname,
174+
staticPaths,
175+
!!opts.cleanStaticGenerated
176+
);
182177

183-
await Promise.all([
184-
fs.promises.writeFile(join(serverOutDir, RESOLVED_STATIC_PATHS_ID), staticPathsCode),
185-
fs.promises.writeFile(
186-
join(serverOutDir, RESOLVED_NOT_FOUND_PATHS_ID),
187-
notFoundPathsCode
188-
),
189-
]);
190-
if (typeof opts.generate === 'function') {
191-
await opts.generate({
192-
outputEntries,
193-
serverOutDir,
194-
clientOutDir,
195-
clientPublicOutDir,
196-
basePathname,
197-
routes,
198-
assetsDir,
199-
warn: (message) => this.warn(message),
200-
error: (message) => this.error(message),
201-
});
202-
}
203-
this.warn(
204-
`\n==============================================` +
205-
`\nNote: Make sure that you are serving the built files with proper cache headers.` +
206-
`\nSee https://qwik.dev/docs/deployments/#cache-headers for more information.` +
207-
`\n==============================================`
208-
);
178+
if (typeof opts.generate === 'function') {
179+
await opts.generate({
180+
outputEntries,
181+
serverOutDir,
182+
clientOutDir,
183+
clientPublicOutDir,
184+
basePathname,
185+
routes,
186+
assetsDir,
187+
warn: (message) => this.warn(message),
188+
error: (message) => this.error(message),
189+
});
209190
}
191+
192+
this.warn(
193+
`\n==============================================` +
194+
`\nNote: Make sure that you are serving the built files with proper cache headers.` +
195+
`\nSee https://qwik.dev/docs/deployments/#cache-headers for more information.` +
196+
`\n==============================================`
197+
);
210198
}
211199
},
212200
},
@@ -288,15 +276,3 @@ export interface AdapterSSGOptions extends Omit<SsgRenderOptions, 'outDir' | 'or
288276
*/
289277
origin?: string;
290278
}
291-
292-
/** @public */
293-
export const STATIC_PATHS_ID = '@qwik-router-static-paths';
294-
295-
/** @public */
296-
export const RESOLVED_STATIC_PATHS_ID = `${STATIC_PATHS_ID}.js`;
297-
298-
/** @public */
299-
export const NOT_FOUND_PATHS_ID = '@qwik-router-not-found-paths';
300-
301-
/** @public */
302-
export const RESOLVED_NOT_FOUND_PATHS_ID = `${NOT_FOUND_PATHS_ID}.js`;

0 commit comments

Comments
 (0)