Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

<script>'s in <svelte:head> are running twice (unless wrapped in {@html}) #1124

@notdotscott

Description

@notdotscott

Describe the bug
Adding a <script> tag to <svelte:head> causes the script to be run twice. I believe this is down to Sapper generating the SSR code and then later on hydrating the page with the same element. However, wrapping the script in a @html directive only runs the script once (it looks like the hydration isn't duplicating the element).

Logs
N/A

To Reproduce

I have created a small repo (using sapper-template) that reproduces the issue: https://github.com/notscottthompson/sapper-head-script-test

In the <Nav> component I have added the following:

<svelte:head>
  <script>console.log('hello from Nav');</script>
  <script src='some-script.js'></script>
  {@html '<script src="some-other-script.js"></script>'}
</svelte:head>

some-script.js logs "hello from some-script.js".
some-other-script.js logs "hello from some-other-script.js".

The following is logged in the browser when loading the test page:

hello from Nav
hello from some-script.js
hello from some-other-script.js
hello from Nav
hello from some-script.js

Expected behavior
I would expect the following to be logged:

hello from Nav
hello from some-script.js
hello from some-other-script.js

Stacktraces
N/A

Information about your Sapper Installation:

  • Your browser and the version: Chrome 80.0

  • Your operating system: Ubuntu Linux 18.04

  • Your hosting environment: Local

  • Sapper version: 0.27.10

  • Svelte version: 3.20.1

  • If it is an exported (npm run export) or dynamic application: dynamic (npm run dev)

  • Whether your application uses Webpack or Rollup: Rollup

Severity
Only mildly annoying. However, due to not being an expert in Svelte/Sapper I'm worried that this may be something I shouldn't be doing (wrapping scripts in @html) or that there may be a better way to achieve it.

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions