Svelte: application/ld+json
Asked Answered
R

3

9

With the following code:

<svelte:head>
    <script type='application/ld+json'>
    {
    "@context": "https://schema.org",
    "@type": "Organization",
    "url": "https://filestar.com",
    "logo": "https://filestar.com/logo-512.png"
    }
    </script>
</svelte:head>

In getting:

[svelte-preprocess] Error transforming 'ld+json'.

Message:
Cannot find module './transformers/ld+json'

Stack:
Error: Cannot find module './transformers/ld+json'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Promise.resolve.then (C:\Repos\Filestar-Website\web-vnext\node_modules\svelte-preprocess\dist\utils.js:112:61)

I've tried the suggestions in this github thread with the same result: https://github.com/sveltejs/svelte/issues/2438

Roily answered 10/11, 2019 at 10:33 Comment(0)
M
11

Looks like this is related to svelte-preprocess. Try adding the preserve: ['ld+json'] option.

Monobasic answered 10/11, 2019 at 14:16 Comment(0)
S
7

For anyone landing here, Rich's answer points you to the right ressource.

What worked for me with Sapper:

// rollup.config.js
// ...
import sveltePreprocess from 'svelte-preprocess';

const preprocess = sveltePreprocess({
  preserve: ['ld+json'],
  // ...
});

export default {
  client: {
    plugins: [
      svelte({
        preprocess,
        // ...
      }),
  },
  server: {
    plugins: [
      svelte({
        preprocess,
        // ...
      }),
    ],
  },
};
<!-- index.svelte -->
<script>
  let jsonld = {
    "@context": "http://schema.org",
    "@type": "...",
    "...": "..."
  };
  jsonld = JSON.stringify(jsonld);
</script>

<svelte:head>
  {@html `<script type="application/ld+json">${jsonld}</script>`}
</svelte:head>

Additional note: Using prettier and prettier-plugin-svelte, the svelte component gets unwanted changes. Doing the following solves my issue.

<!-- index.svelte -->
<script>
  let jsonld = {
    "@context": "http://schema.org",
    "@type": "...",
    "...": "..."
  };
  jsonld = JSON.stringify(jsonld);
  let jsonldScript = `<script type="application/ld+json">${jsonld +
    "<"}/script>`;
</script>

<svelte:head>
  {@html jsonldScript}
</svelte:head>
Symons answered 19/1, 2020 at 11:1 Comment(1)
thanks for that. I ended up using your 2nd index.svelte solution, as it worked where the other method did not.Springe
D
1

Here's another way to do this, according to this comment.

<script>
  const schema = {
    "@context": "https://schema.org",
    "@type": "Organization",
    "url": "https://filestar.com",
    "logo": "https://filestar.com/logo-512.png"
  }
</script>

<svelte:head>
  {@html 
    `
    <script type="application/ld+json">
      ${JSON.stringify(schema)}
    </script>
    `
  }
</svelte:head>
Dirk answered 1/7, 2020 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.