Skip to content

Commit 1d5993e

Browse files
committed
chore: make trailingSlash a build constant
1 parent b607880 commit 1d5993e

19 files changed

+51
-71
lines changed

packages/qwik-router/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ declare var __EXPERIMENTAL__: {
2222

2323
declare var __DEFAULT_LOADERS_SERIALIZATION_STRATEGY__: SerializationStrategy;
2424

25+
/** Should routes not have a trailing slash? */
2526
declare var __NO_TRAILING_SLASH__: boolean;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ function normalizeOptions(
9292
const url = new URL(opts.basePathname, 'https://qwik.dev/');
9393
opts.basePathname = url.pathname;
9494

95-
if (typeof opts.trailingSlash !== 'boolean') {
96-
opts.trailingSlash = true;
97-
}
98-
9995
opts.mdx = opts.mdx || {};
10096
opts.platform = opts.platform || {};
10197

packages/qwik-router/src/buildtime/markdown/markdown-url.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export function getMarkdownRelativeUrl(
4949
}
5050
} else if (extension === '') {
5151
if (url.endsWith('/')) {
52-
if (!opts.trailingSlash) {
52+
if (globalThis.__NO_TRAILING_SLASH__) {
5353
url = url.slice(0, -1);
5454
}
55-
} else if (opts.trailingSlash) {
55+
} else if (!globalThis.__NO_TRAILING_SLASH__) {
5656
url += '/';
5757
}
5858
}

packages/qwik-router/src/buildtime/markdown/markdown-url.unit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ const menuFilePath = join(routesDir, 'docs', 'menu.md');
6666
expect: './getting-started.txt',
6767
},
6868
].forEach((t) => {
69-
test(`getMarkdownRelativeUrl ${t.href}`, () => {
69+
test(`getMarkdownRelativeUrl ${t.href} ${t.trailingSlash ? 'with' : 'without'} slash`, () => {
7070
const opts: NormalizedPluginOptions = {
7171
basePathname: '/',
72-
trailingSlash: !!t.trailingSlash,
7372
routesDir,
7473
serverPluginsDir,
7574
mdxPlugins: {
@@ -82,6 +81,7 @@ const menuFilePath = join(routesDir, 'docs', 'menu.md');
8281
rewriteRoutes: [],
8382
defaultLoadersSerializationStrategy: 'never',
8483
};
84+
globalThis.__NO_TRAILING_SLASH__ = !t.trailingSlash;
8585
assert.equal(getMarkdownRelativeUrl(opts, menuFilePath, t.href), t.expect);
8686
});
8787
});

packages/qwik-router/src/buildtime/routing/resolve-source-file.unit.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ test('resolveLayout', () => {
4040
routesDir: '',
4141
serverPluginsDir: '',
4242
basePathname: '/',
43-
trailingSlash: false,
4443
mdxPlugins: {
4544
remarkGfm: true,
4645
rehypeSyntaxHighlight: true,

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ export function generateQwikRouterConfig(
2424

2525
createEntries(ctx, c);
2626

27-
c.push(`export const trailingSlash = ${JSON.stringify(!!ctx.opts.trailingSlash)};`);
27+
c.push(`export const trailingSlash = ${JSON.stringify(!globalThis.__NO_TRAILING_SLASH__)};`);
2828

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

3131
c.push(`export const cacheModules = ${JSON.stringify(!ctx.isDevServer)};`);
3232

33-
c.push(
34-
`export default { routes, serverPlugins, menus, trailingSlash, basePathname, cacheModules };`
35-
);
33+
c.push(`export default { routes, serverPlugins, menus, basePathname, cacheModules };`);
3634

3735
return esmImports.join('\n') + c.join('\n');
3836
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export interface MdxPlugins {
145145
rehypeAutolinkHeadings: boolean;
146146
}
147147

148-
export interface NormalizedPluginOptions extends Required<PluginOptions> {
148+
export interface NormalizedPluginOptions extends Omit<Required<PluginOptions>, 'trailingSlash'> {
149149
assetsDir?: string;
150150
}
151151

packages/qwik-router/src/buildtime/vite/dev-server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function ssrDevMiddleware(ctx: BuildContext, server: ViteDevServer) {
4444
return { route, params };
4545
}
4646

47-
if (ctx.opts.trailingSlash && !pathname.endsWith('/')) {
47+
if (!globalThis.__NO_TRAILING_SLASH__ && !pathname.endsWith('/')) {
4848
params = matchRoute(route.pathname, pathname + '/');
4949
if (params) {
5050
return { route, params };
@@ -129,7 +129,7 @@ export function ssrDevMiddleware(ctx: BuildContext, server: ViteDevServer) {
129129
return { serverPlugins, loadedRoute };
130130
};
131131
const resolveRoute = (routeModulePaths: WeakMap<RouteModule<unknown>, string>, url: URL) => {
132-
const matchPathname = getRouteMatchPathname(url.pathname, ctx.opts.trailingSlash);
132+
const matchPathname = getRouteMatchPathname(url.pathname);
133133
routePs[matchPathname] ||= _resolveRoute(routeModulePaths, matchPathname).finally(() => {
134134
delete routePs[matchPathname];
135135
});
@@ -251,7 +251,6 @@ export function ssrDevMiddleware(ctx: BuildContext, server: ViteDevServer) {
251251
loadedRoute,
252252
requestHandlers,
253253
rebuildRouteInfo,
254-
ctx.opts.trailingSlash,
255254
ctx.opts.basePathname,
256255
qwikSerializer
257256
);

packages/qwik-router/src/middleware/request-handler/request-event.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const RequestEvQwikSerializer = Symbol('RequestEvQwikSerializer');
3737
export const RequestEvLoaderSerializationStrategyMap = Symbol(
3838
'RequestEvLoaderSerializationStrategyMap'
3939
);
40-
export const RequestEvTrailingSlash = Symbol('RequestEvTrailingSlash');
4140
export const RequestRouteName = '@routeName';
4241
export const RequestEvSharedActionId = '@actionId';
4342
export const RequestEvSharedActionFormData = '@actionFormData';
@@ -51,7 +50,6 @@ export function createRequestEvent(
5150
serverRequestEv: ServerRequestEvent,
5251
loadedRoute: LoadedRoute | null,
5352
requestHandlers: RequestHandler<any>[],
54-
trailingSlash: boolean,
5553
basePathname: string,
5654
qwikSerializer: QwikSerializer,
5755
resolved: (response: any) => void
@@ -64,7 +62,7 @@ export function createRequestEvent(
6462
const url = new URL(request.url);
6563
if (url.pathname.endsWith(QDATA_JSON)) {
6664
url.pathname = url.pathname.slice(0, -QDATA_JSON_LEN);
67-
if (trailingSlash && !url.pathname.endsWith('/')) {
65+
if (!globalThis.__NO_TRAILING_SLASH__ && !url.pathname.endsWith('/')) {
6866
url.pathname += '/';
6967
}
7068
sharedMap.set(IsQData, true);
@@ -155,7 +153,6 @@ export function createRequestEvent(
155153
[RequestEvLoaders]: loaders,
156154
[RequestEvLoaderSerializationStrategyMap]: new Map(),
157155
[RequestEvMode]: serverRequestEv.mode,
158-
[RequestEvTrailingSlash]: trailingSlash,
159156
get [RequestEvRoute]() {
160157
return loadedRoute;
161158
},
@@ -339,7 +336,6 @@ export interface RequestEventInternal extends RequestEvent, RequestEventLoader {
339336
[RequestEvLoaders]: Record<string, ValueOrPromise<unknown> | undefined>;
340337
[RequestEvLoaderSerializationStrategyMap]: Map<string, SerializationStrategy>;
341338
[RequestEvMode]: ServerRequestMode;
342-
[RequestEvTrailingSlash]: boolean;
343339
[RequestEvRoute]: LoadedRoute | null;
344340
[RequestEvQwikSerializer]: QwikSerializer;
345341

@@ -372,10 +368,6 @@ export function getRequestLoaderSerializationStrategyMap(requestEv: RequestEvent
372368
return (requestEv as RequestEventInternal)[RequestEvLoaderSerializationStrategyMap];
373369
}
374370

375-
export function getRequestTrailingSlash(requestEv: RequestEventCommon) {
376-
return (requestEv as RequestEventInternal)[RequestEvTrailingSlash];
377-
}
378-
379371
export function getRequestRoute(requestEv: RequestEventCommon) {
380372
return (requestEv as RequestEventInternal)[RequestEvRoute];
381373
}

packages/qwik-router/src/middleware/request-handler/request-handler.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function requestHandler<T = unknown>(
3333
}
3434

3535
const pathname = serverRequestEv.url.pathname;
36-
const matchPathname = getRouteMatchPathname(pathname, qwikRouterConfig.trailingSlash);
36+
const matchPathname = getRouteMatchPathname(pathname);
3737
const routeAndHandlers = await loadRequestHandlers(
3838
qwikRouterConfig,
3939
matchPathname,
@@ -46,7 +46,7 @@ export async function requestHandler<T = unknown>(
4646
const [route, requestHandlers] = routeAndHandlers;
4747

4848
const rebuildRouteInfo: RebuildRouteInfoInternal = async (url: URL) => {
49-
const matchPathname = getRouteMatchPathname(url.pathname, qwikRouterConfig.trailingSlash);
49+
const matchPathname = getRouteMatchPathname(url.pathname);
5050
const routeAndHandlers = await loadRequestHandlers(
5151
qwikRouterConfig,
5252
matchPathname,
@@ -68,7 +68,6 @@ export async function requestHandler<T = unknown>(
6868
route,
6969
requestHandlers,
7070
rebuildRouteInfo,
71-
qwikRouterConfig.trailingSlash,
7271
qwikRouterConfig.basePathname,
7372
qwikSerializer
7473
);

0 commit comments

Comments
 (0)