kit icon indicating copy to clipboard operation
kit copied to clipboard

[docs] +server does not inherit from +layout

Open sacrosanctic opened this issue 1 year ago • 1 comments

Describe the bug

As far as I can tell, +server does not inherit from +layout. Propose to increase the distinction between +server and the other +files.

For example, an auth guard place in routes/(app)/+layout.server.ts does not protect routes/(app)/api/+server.ts. (relevant discord thread)

An inverse example when using [email protected], it also affects which +page.(server).ts get loaded.

To me, it shows that of the 4 +files, +server stands alone. Aside from the fact that it uses folders to determine its route, it has nothing to do with the other 3 +files, and don't inherit from any of them.

But the docs seem to imply that +server is a part of the family in the way that it is presented:

Reproduction

.

Logs

No response

System Info

.

Severity

annoyance

Additional Information

No response

sacrosanctic avatar Jun 24 '24 20:06 sacrosanctic

No, it doesn't, because +server is for REST endpoints and has no bearing on +layout which is for page rendering (and vice versa). +layout.server can access the same server-side event props that +server can, that's the only similarity.

They only reside together because that is how routes are defined. +server is really apart from the other files because it's usually rendering JSON (although it can render anything - XML sitemaps, PDF files etc..), whereas all the others are concerned with rendering pages, either the HTML or the data that the page needs.

As for security, expecting any checks in +layout.server to protect any routes underneath is a mistake, unless they all await the parent load fn which can be a performance killer.

The rest of the page does make the distinction between "pages", but it could maybe change that initial wording to something like:

"+layout and +error files apply to pages in subdirectories as well as pages in the directory they live in"

CaptainCodeman avatar Jun 25 '24 19:06 CaptainCodeman