Skip to content
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

[docs] rewrite load docs #7174

Merged
merged 24 commits into from
Oct 21, 2022
Merged

[docs] rewrite load docs #7174

merged 24 commits into from
Oct 21, 2022

Conversation

dummdidumm
Copy link
Member

Superseedes #7087
Closes #7148

This rewrites the load docs in a way that is more grouped around use cases and problems you want to solve. It also adds API docs for the load functions.

While I was at it, I also added a link checker to check for broken links, which promptly found some outdated links which I also udpated.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Oct 7, 2022

🦋 Changeset detected

Latest commit: 18a3370

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dummdidumm dummdidumm mentioned this pull request Oct 7, 2022
5 tasks
- you are building an SPA served from a static file server
- use both `load` functions in case you have a combination of requirements

### Making fetch requests
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Conduitry in #7113 you mentioned you wanted some more details about the subtle difference of fetch and when to use it - this would be the section to add it to.

Copy link
Member

@geoffrich geoffrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big improvement, thanks!

documentation/docs/05-load.md Outdated Show resolved Hide resolved
documentation/docs/05-load.md Outdated Show resolved Hide resolved
documentation/docs/05-load.md Outdated Show resolved Hide resolved
documentation/docs/05-load.md Outdated Show resolved Hide resolved
documentation/docs/05-load.md Outdated Show resolved Hide resolved
documentation/docs/05-load.md Outdated Show resolved Hide resolved
SvelteKit tracks the dependencies of each `load` function to avoid re-running it unnecessarily during navigation. For example, a `load` function in a root `+layout.js` doesn't need to re-run when you navigate from one page to another unless it references `url` or a member of `params` that changed since the last navigation.
SvelteKit tracks the dependencies of each `load` function to avoid re-running it unnecessarily during navigation. For example, a `load` function in a root `+layout.js` doesn't need to re-run when you navigate from one page to another unless it references `url` or a member of `params` that changed since the last navigation. `load` functions also take into account parent `load` functions, if you reference them through `await parent()` - in this case, if an upper `load` function is rerun, so is the `load` function that does `await parent()`.

`load` functions do not only run when you navigate, they can also be triggered to rerun by `invalidate` or `invalidateAll` (which we get to in the next paragraph). `invalidate` is closely connected to `fetch` and `depends`, which are both methods passed to `load`. Making a `fetch` request automatically registers the fetched URL as a dependency of the load function. `depends` does the same in a manual way and registers the specified URL as a dependency. You can then use `invalidate` and pass one of the registered URLs (or a function, if you need to decide based on a pattern rather than the full URL) to it to rerun the `load` function:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About depends. It took a while this morning for me to figure out that you call depends in the load you are depending on, not elsewhere (like a load further down the tree.) Maybe this was just because it was my first time using it. I'm fine with the word depends now, but maybe we can point out that it means (AFIACT) "define a cache key for this load function."

Co-authored-by: Geoff Rich <4992896+geoffrich@users.noreply.github.com>
Co-authored-by: Chris Carson <chris@nowzoo.com>
@Rich-Harris
Copy link
Member

Cannot for the life of me figure out why this is failing to deploy. Builds fine locally

@Rich-Harris
Copy link
Member

Ah, I tell a lie. Running build inside packages/kit to update the type definitions causes sites/kit.svelte.dev to fail to build

@Rich-Harris
Copy link
Member

This is good stuff, but I can't shake the feeling that it doesn't belong in reference docs. It's a guide. Maybe we should move it to learn.svelte.dev?

@dummdidumm
Copy link
Member Author

For me learn.svelte.dev is the place for

  • the tutorial (even that I'm not 100% sure on from time to time)
  • recipes (how to write your own X)
  • guides (how to do auth)

For me this is not a guide in that sense.

What I envision for kit.svelte.dev to have is

  • API docs (the thing that's currently split up in types and modules, and which we probably should merge and enhance)
  • introductionary docs (later with some cross links to API docs, tutorial, guides) -> this is this rewrite

One thing that I noticed from lurking in the questions channel on Discord is that people have a hard time to understand how to actually use the parts we give them together, and this rewrite tries to answer that for the load function. API or pure reference docs can't solve that, the tutorial can't either because it does things piece by piece instead of giving an overview in one reading, which is why I think a middle ground between tutorial and API docs is needed. The routing docs for example do this nicely because they clearly show the use case and how things fit together. I tried something similar with these docs. Previously the pieces on that page were too disconnected to make sense of them as a whole.

@Rich-Harris
Copy link
Member

Fair enough — in that case we should probably start thinking about what a better API section would look like.

I have some edits locally (since the GitHub UI is a bad place to make prose edits), will push them up tomorrow

@dummdidumm dummdidumm mentioned this pull request Oct 20, 2022
5 tasks
documentation/docs/05-load.md Outdated Show resolved Hide resolved

Be careful not to introduce accidental waterfalls when using `await parent()`. If for example you only want to merge parent data into the returned output, call it _after_ fetching your other data.
### Cookies and headers
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason to switch explaining headers and cookies around in this section? My arguments for headers then cookies are:

  • cookies is a special case of headers
  • that "you cannot set cookies in setHeaders use this instead" reads more naturally in that order

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically that cookies aren't just a special case of headers — cookies.set(...) causes a set-cookie header to be appended, but it also updates the cookie store that accompanies the RequestEvent. Meanwhile cookies.get(...) is nothing to do with response.headers, it reads from request.headers and the event's cookie store. Headers are the mechanism for dealing with cookies, but I think it's helpful to treat them as somewhat conceptually different — most headers just affect the response, but set-cookie affects the user agent.

In general I think it's good to be able to refer back to things that were previously covered, so that you're layering knowledge. Otherwise someone reads about setHeader, thinks 'great, I can use this to set a cookie', then learns that actually no, they have to learn this whole other thing.

documentation/docs/05-load.md Outdated Show resolved Hide resolved
documentation/docs/05-load.md Show resolved Hide resolved
documentation/docs/05-load.md Show resolved Hide resolved
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
@Rich-Harris Rich-Harris merged commit 250f75c into master Oct 21, 2022
@Rich-Harris Rich-Harris deleted the load-docs-v2 branch October 21, 2022 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Documentation of Advanced Layout Features
5 participants