-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Consider how to pass options between handlers in sequence #4051
Comments
transformPage
not working in sequencetransformPage
not working properly in sequence
Yeah, this is a broader issue around passing options between handlers. I think we probably want to change the behaviour of /**
* @param {...import('types').Handle} handlers
* @returns {import('types').Handle}
*/
export function sequence(...handlers) {
const length = handlers.length;
if (!length) return ({ event, resolve }) => resolve(event);
return ({ event, resolve }) => {
return apply_handle(0, event);
/**
* @param {number} i
* @param {import('types').RequestEvent} event
* @returns {import('types').MaybePromise<Response>}
*/
- function apply_handle(i, event) {
+ function apply_handle(i, event, options = {}) {
const handle = handlers[i];
return handle({
event,
- resolve: i < length - 1 ? (event) => apply_handle(i + 1, event) : resolve
+ resolve: i < length - 1 ? (event, options) => apply_handle(i + 1, event, options) : resolve,
+ options
});
}
};
} ...which would change the API to this: import { sequence } from '@sveltejs/kit/hooks';
/** @type {import('@sveltejs/kit').Handle} */
async function first({ event, resolve }) {
console.log('first pre-processing');
- const result = await resolve(event);
+ const result = await resolve(event, { transformPage });
console.log('first post-processing');
return result;
}
/** @type {import('@sveltejs/kit').Handle} */
-async function second({ event, resolve }) {
+async function second({ event, resolve, options }) {
console.log('second pre-processing');
- const result = await resolve(event);
+ const result = await resolve(event, options);
console.log('second post-processing');
return result;
}
export const handle = sequence(first, second); Another idea we discussed in the maintainers' chat recently was to automatically merge options on the way 'down', but I think I prefer the explicit approach. |
Thanks for the explanation! My 2 cents: It seems to me that having to explicitly pass the options to every other handles would become cumbersome if you have a lot of them. Moreover, I also don't think automatically merging the options would remove any flexibility for the user. |
transformPage
not working properly in sequence
Describe the bug
When using the
sequence
helper to chain multiple handle functions,transformPage
is only actually run in the last one. Not sure if it was intended or not, but it would be nice if we could usetransformPage
in all handles, regardless of order.Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-wg5zme?file=src%2Fhooks.js&terminal=dev
You can see in the reproduction that the html
lang
attribute is not changed. If we swap the two handle functions so that the one that usestransformPage
comes last, the html is changed.Logs
No response
System Info
Severity
serious, but I can work around it
Additional Information
No response
The text was updated successfully, but these errors were encountered: