Skip to content

Server-side rendering in Svelte #1759

Answered by travishorn
beingflo asked this question in Q&A
Discussion options

You must be logged in to vote

I was successful in forcing the plot to be generated server-side by creating a +page.server.js:

import * as Plot from '@observablehq/plot';
import { JSDOM } from 'jsdom';

const { document } = new JSDOM().window;

/** @type {import('./$types').PageServerLoad} */
export async function load() {
  const plot = Plot.plot({
    marks: [
      Plot.rectY(
        { length: 10000 },
        Plot.binX({ y: "count" }, { x: Math.random })
      )
    ],
    document
  }).outerHTML;

  return {
    plot
  };
}

Then, pick up the HTML string on +page.svelte:

<script>
  /** @type {import('./$types').PageData} */
  export let data;
</script>

{@html data.plot}

FYI, I tried placing the rendering code in +…

Replies: 7 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@travishorn
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by mbostock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
documentation Improvements or additions to docs question Further information is needed
5 participants
Converted from issue

This discussion was converted from issue #1550 on July 19, 2023 16:25.