Creating Server Pages
In previous examples, we’ve used the +page.js
and +page.server.js
files for loading data. Often, they can be used interchangeably, but when is the best time to use which? In this section, we’ll break down some of the differences between the two and also discuss various features available in the +page.server.js
files. We’ll break it down into these topics:
load()
- Page options
- Actions
load()
As we’ve seen in previous examples, data can be loaded into a +page.svelte
component by exporting the data
property on that page. Both +page.js
and +page.server.js
can then be used for loading data to that page template as they can both export a load()
function. When to use which file depends on how you plan to load that data. When run in a +page.js
file, load()
will run on both the client and the server. It is recommended to load data here if you are able, as SvelteKit can manage grabbing data from calls with fetch()
. This...