Skip to content
GuidesChecked 18 November 2026

Add a feedback button to a SvelteKit app

Load the script in app.html, bind the popup in onMount so it only runs in the browser.

Free for one project and 100 votes. Paid plans start with a 14 day trial, no card.

The server problem

VoteFirst lives on window and binds to a real button. Both exist only after the page reaches the browser.

On the server there is no window, so the call has to wait for the browser.

onMount runs there and only there.

A SvelteKit page runs its script twice in a sense: once on the server to produce HTML, then again in the browser to make that HTML interactive. A call to window.VoteFirst written at the top of a component would fail on the server, where window does not exist.

The onMount function from Svelte is called only in the browser, after the component's markup is on the page. That makes it the one place where the button exists and the script is loaded.

Load the script

The app.html file wraps every page. Put the VoteFirst tag in its head, with your slug and key as data attributes.

Add the tag to src/app.html, before the page body.

Svelte adds its own head content through a placeholder.

The tag is a plain script, so it stops the parser and runs before the rest of the page. By the time onMount fires, window.VoteFirst is defined, and no loading code is needed.

Take the slug and key from the Share and Embed screen of your project. The key can sit in public markup, because it only opens the form and takes votes from origins you have listed.

src/app.html
<head>
  <meta charset="utf-8" />
  <script src="https://app.votefirst.app/embed/votefirst.js" data-project="your-slug" data-key="your-key"></script>
  %sveltekit.head%
</head>

The component

A reference to the button goes to VoteFirst.suggest. The function you return from onMount runs when the component is destroyed.

Bind with bind:this and return the cleanup from onMount.

That returned function calls destroy on the popup.

Svelte documents that a function returned from onMount is called when the component unmounts. Returning destroy there means a user who leaves the page takes the click listener with them.

Without the return, each visit to the page adds another listener to a fresh button. The old buttons are gone, so nothing would visibly break, but an open dialog from a stale binding can linger.

src/lib/FeedbackButton.svelte
<script>
  import { onMount } from 'svelte';

  let button;

  onMount(() => {
    const popup = window.VoteFirst.suggest({ trigger: button });
    return () => popup.destroy();
  });
</script>

<button bind:this={button} type="button">Feedback</button>

Where it goes

A button in src/routes/+layout.svelte stays mounted while users move between routes, so it binds once.

Put it in the root layout to have it on every page.

A button in one route binds each time that route opens.

SvelteKit keeps the layout alive during client side navigation and swaps only the page inside it. A layout button is therefore bound a single time for a whole visit.

A route level button suits a screen where the ask is specific, such as a settings page or an empty list. Both placements are safe, because the cleanup runs when each one leaves.

src/routes/+layout.svelte
<script>
  import FeedbackButton from '$lib/FeedbackButton.svelte';
</script>

<nav>
  <FeedbackButton />
</nav>

<slot />

The board

The popup takes an idea. The public board lists every idea with its status, and a visitor can vote with no account.

Send users to a board where ideas are counted.

A suggestion stays hidden until you approve it.

Link the board from your footer or your help menu. Someone who opens it sees the card they were about to write, adds a vote, and leaves. That keeps ten users from filing the same wish in ten different ways.

Cards that overlap can be merged, and the votes of both land on the card you keep. A person who voted on each one still counts once. The board can use your own subdomain on the Free and Pro plans.

If it fails

If the terminal shows that window is not defined, the call sits outside onMount. If the page loads but the click does nothing, look at the origin list.

A window error means the call ran too early.

The console names a refused origin as origin_not_allowed.

Move the call into onMount and the server error goes away. Check that the script tag is in app.html and that your browser has not blocked it, since some content blockers hide scripts from third parties.

List every address you use on Share and Embed, including localhost with the port your dev server picks, or the form will refuse to open while you work.

If the button works in development and not in a build, check that the script tag is in the built HTML and that the call sits inside onMount. Code outside it also runs on the server, where there is no window. After a change, stop the dev server and start it again, so an old module is not served from memory.

What a SvelteKit app gains

Safe on the server

The call lives in onMount, so server rendering never meets window.

A button that lets go

The returned function removes the listener when the component is destroyed.

Ideas you can rank

Repeat requests become votes on one card, and the top of the list shows demand.

Add it in five steps

One tag in app.html and one small component do the whole job.

Get the slug and key

Create a project on the Free plan and open Share and Embed.

Edit app.html

Place the script tag in the head, above the SvelteKit head placeholder.

Write the component

Create FeedbackButton.svelte with bind:this and onMount.

Mount it in the layout

Render the component in +layout.svelte so every route has it.

List your origins

Add the live address and the dev server address to the key.

A check before you ship

Build the app and run the preview, then open the page in a private window. Confirm that the button appears, the popup opens and the browser console shows no window error.

Add the production address to the list of origins and remove the local one.

What to know before you set this up

  • The embed script was read at version 2.8.1, so check your dashboard if a later release adds or renames an option.
  • The call must stay inside onMount, since the server has no window.
  • Only listed origins may open the form, and a dev server on a new port needs adding.
  • The Free plan stops at 100 votes, and a vote taken back does not give its slot back.

What SvelteKit developers ask

Does this work with Svelte 4 as well as Svelte 5?

Yes. The component uses onMount, bind:this and a returned cleanup, and all three behave the same in both versions.

Is there an npm package to install?

No. VoteFirst is one hosted file loaded through a script tag, so the project gains no dependency and no build step.

Why not call it at the top of the script block?

That code runs on the server during rendering, where window is missing. onMount runs only in the browser.

Will it work with prerendered pages?

Yes. A prerendered page ships static HTML, and onMount binds the button when the page loads in the browser.

What does destroy do?

It detaches the click listener from the button and closes the dialog if one is open.

Does the key have to stay private?

No. It is meant to be public and can only open the form and take votes from origins on its list.

Do users need an account to vote?

Not on a new board. The visitor's own browser storage recognizes a return visit, and required sign in is available when each vote must name a person.

Can the board use a subdomain of my site?

Yes, on the Free and Pro plans. Point a name such as feedback.yourapp.com at the board.

Where do new suggestions show up?

They arrive in your dashboard as cards waiting for approval, and the owner gets a single email a day at most.

What does it cost to start?

The Free plan holds one project and 100 votes. Lite is $9/mo and Pro is $39/mo, and both can be tried for 14 days with no card.

Hear what users want before you build it

Free covers a first project with 100 votes, and Lite or Pro can be tried for 14 days without a card.