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: improve SvelteKit docs #514

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 30 additions & 44 deletions docs/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,12 @@ title: SvelteKit

SvelteKit uses Vite to build, so we can use `partytownVite`.

1. Add the Partytown script to `src/routes/+layout.svelte`
2. Copy the Partytown files to the local filesystem using the Vite plugin
3. Optional: reverse-proxying scripts
4. Optional: `svelte-preprocess` configuration
5. Then adding 3rd party scripts

## 1. Add the Partytown script to `src/routes/+layout.svelte`
1. Copy the Partytown files to the local filesystem using the Vite plugin
2. Add the Partytown script to `src/routes/+layout.svelte`
3. Then add 3rd party scripts
4. Optional: reverse-proxying scripts

Adapting from [the HTML integration guide](https://partytown.builder.io/html)

```svelte
// src/routes/+layout.svelte

<script>
import { partytownSnippet } from '@builder.io/partytown/integration'
</script>

<svelte:head>
<!-- Config options -->
<script>
// Forward the necessary functions to the web worker layer
partytown = {
forward: ['dataLayer.push']
}
</script>

{@html '<script>' + partytownSnippet() + '</script>'}
</svelte:head>
```

## 2. Copy the Partytown files to the local filesystem using the Vite plugin
## 1. Copy the Partytown files to the local filesystem using the Vite plugin

Adopting [this strategy](https://partytown.builder.io/copy-library-files#vite) from the Partytown + Vite docs:

Expand All @@ -56,27 +31,33 @@ const config = {
export default config
```

## 3. Optional: reverse-proxying scripts
## 2. Add the Partytown script to `src/routes/+layout.svelte`

This will only be necessary depending on which scripts you are using. The implementation will vary depending on hosting platform. See [Partytown's recommended guides](https://partytown.builder.io/proxying-requests#reverse-proxy).
Adapting from [the HTML integration guide](https://partytown.builder.io/html)

## 4. Optional: `svelte-preprocess` configuration
```svelte
// src/routes/+layout.svelte

Most users will be using `vitePreprocess` and will not need to update their `svelte.config.js` file. However, if you're are using `svelte-preprocess`, you will need to set the `preserve` option:
```js
// svelte.config.js
<script>
import { partytownSnippet } from '@builder.io/partytown/integration'
</script>

const config = {
preprocess: preprocess({
preserve: ['partytown']
})
...
}
<svelte:head>
<!-- Config options -->
<script>
// Forward the necessary functions to the web worker layer
partytown = {
forward: ['dataLayer.push']
}
</script>

{@html '<script>' + partytownSnippet() + '</script>'}
</svelte:head>
```

## 5. Then adding 3rd party scripts
## 3. Then add 3rd party scripts

This is where we FINALLY use partytown to add those scripts (note `type="text/partytown"` below). This example shows Google Tag Manager. Putting it together with the previous changes, our `+layout.svelte` looks like:
This is where we use partytown to add those scripts (note `type="text/partytown"` below). This example shows Google Tag Manager. Putting it together with the previous changes, our `+layout.svelte` looks like:

```svelte
// src/routes/+layout.svelte
Expand Down Expand Up @@ -106,4 +87,9 @@ This is where we FINALLY use partytown to add those scripts (note `type="text/pa
</svelte:head>
```

## 4. Optional: reverse-proxying scripts

This will only be necessary depending on which scripts you are using. The implementation will vary depending on hosting platform. See [Partytown's recommended guides](https://partytown.builder.io/proxying-requests#reverse-proxy).


Acknowledgements: credit belongs to monogram.io for [an earlier version of this guide](https://monogram.io/blog/add-partytown-to-svelte).
Loading