Skip to content

Commit

Permalink
[fix] match regex against route only once
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Aug 14, 2021
1 parent c5efbf3 commit 1c279fb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
8 changes: 2 additions & 6 deletions packages/kit/src/runtime/server/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ function is_string(s) {
/**
* @param {import('types/hooks').ServerRequest} request
* @param {import('types/internal').SSREndpoint} route
* @param {RegExpExecArray} match
* @returns {Promise<import('types/hooks').ServerResponse | undefined>}
*/
export async function render_endpoint(request, route) {
export async function render_endpoint(request, route, match) {
const mod = await route.load();

/** @type {import('types/endpoint').RequestHandler} */
Expand All @@ -30,11 +31,6 @@ export async function render_endpoint(request, route) {
return;
}

const match = route.pattern.exec(request.path);
if (!match) {
return error('could not parse parameters from request path');
}

const params = route.params(match);

const response = await handler({ ...request, params });
Expand Down
7 changes: 4 additions & 3 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ export async function respond(incoming, options, state = {}) {

const decoded = decodeURI(request.path);
for (const route of options.manifest.routes) {
if (!route.pattern.test(decoded)) continue;
const match = route.pattern.test(decoded);
if (!match) continue;

const response =
route.type === 'endpoint'
? await render_endpoint(request, route)
: await render_page(request, route, options, state);
? await render_endpoint(request, route, match)
: await render_page(request, route, match, options, state);

if (response) {
// inject ETags for 200 responses
Expand Down
5 changes: 2 additions & 3 deletions packages/kit/src/runtime/server/page/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { respond } from './respond.js';
/**
* @param {import('types/hooks').ServerRequest} request
* @param {import('types/internal').SSRPage} route
* @param {RegExpExecArray} match
* @param {import('types/internal').SSRRenderOptions} options
* @param {import('types/internal').SSRRenderState} state
* @returns {Promise<import('types/hooks').ServerResponse | undefined>}
*/
export async function render_page(request, route, options, state) {
export async function render_page(request, route, match, options, state) {
if (state.initiator === route) {
// infinite request cycle detected
return {
Expand All @@ -17,8 +18,6 @@ export async function render_page(request, route, options, state) {
};
}

const match = route.pattern.exec(request.path);
// @ts-expect-error we already know there's a match
const params = route.params(match);

const page = {
Expand Down

0 comments on commit 1c279fb

Please sign in to comment.