kit icon indicating copy to clipboard operation
kit copied to clipboard

The default location for hooks should be a directory

Open lubiah opened this issue 3 years ago • 3 comments

Describe the problem

In the previous version of Sveltekit, the default location for hooks was src/hooks but after some time when it was divided into both client and server hooks, the directory was removed. I know we can all configure the location in the config file but I suggest we stick to src/hooks where we can place both hooks.server.js and hooks.client.js inside. Doing this makes the project looks more organized because we are grouping similar content together. Even service workers who usually contain just an index.js file gets to use a whole directory why not group both hooks file into a single directory, this was the default before and I do not understand why it was removed.

Describe the proposed solution

The default path for hooks should be src/hooks

Alternatives considered

We can edit the configuration files ourselves to change it

Importance

would make my life easier

Additional Information

No response

lubiah avatar Nov 29 '22 22:11 lubiah

I think it would be an idea to have the hooks function within the layout.ts or layout.server.ts as a separate function like export const hook = async () => {} as it applies to the directory it is in, this would remove one extra file needed and collate information to make it simpler for the user

bobbymannino avatar Nov 30 '22 12:11 bobbymannino

I think it would be an idea to have the hooks function within the layout.ts or layout.server.ts as a separate function like export const hook = async () => {} as it applies to the directory it is in, this would remove one extra file needed and collate information to make it simpler for the user

how is this different from a load function exported from the layout?

(leaving this open until we reach a conclusion; I'm against this - either way, it's a 2.0 issue if anything)

dummdidumm avatar Dec 12 '22 17:12 dummdidumm

I think it would be an idea to have the hooks function within the layout.ts or layout.server.ts as a separate function like export const hook = async () => {} as it applies to the directory it is in, this would remove one extra file needed and collate information to make it simpler for the user

how is this different from a load function exported from the layout?

The page load function may be triggered at the same time the page load function does. You could always await parent() in the page load function but, aside of being easy to forget, the layout load function may do a lot of extra stuff you don't need for the hooks.

Instead of the export const hook = async () => {} in the layout, I'd rather have a +hooks.server.ts file per directory route so that you can avoid messing with route ids like so:

export const handle = async ({ event, resolve }) => {
	if (event.route.id?.startsWith('/(protected)')) {
		...
	}
	else {
		...
	}
	return await resolve(event);
};

I guess for nested +hook.server.ts the file router could apply automatically the hooks sequence function, but it could get quite tricky if you need to control the application. Perhaps something analogous to layout groups could work, like +hooks.server@(protected).ts.

jdnavarro avatar Oct 06 '23 13:10 jdnavarro

We're leaving this as is - closing.

dummdidumm avatar Dec 12 '23 19:12 dummdidumm