最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

svelte - Sveltekit SPA mode error handling in <script> tags - Stack Overflow

programmeradmin0浏览0评论

If you have a sveltekit project configured in SPA mode: export const ssr = false; in the +layout.ts, how do you handle an exception being thrown from the <script> tags:

<!-- src/routes/test/+page.svelte -->
<script lang="ts">
  throw new Error('hey');
</script>

If you have a sveltekit project configured in SPA mode: export const ssr = false; in the +layout.ts, how do you handle an exception being thrown from the <script> tags:

<!-- src/routes/test/+page.svelte -->
<script lang="ts">
  throw new Error('hey');
</script>
Share Improve this question asked Jan 18 at 11:55 Robert MihaiRobert Mihai 3743 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I am not aware of a way to catch that in a +layout.ts. There is a handleError hook, but that would be more global and does not seem to capture errors thrown during initialization.

Without SSR, a boundary could be used in the +layout.svelte to show an error, though:

<svelte:boundary>
  {@render children()}

  {#snippet failed(error, reset)}
    <p>Something went wrong</p>

    <button onclick={reset}>Retry</button>
  {/snippet}
</svelte:boundary>

(Requires at least Svelte 5.3.0)

You can listen for uncaught errors by listen for the error event on the window object.

window.addEventListener("error", (event) => {
  log.textContent = `${log.textContent}${event.type}: ${event.message}\n`
  console.log(event)
})

But perhaps the other provided answer is more suitable for your specific case.

发布评论

评论列表(0)

  1. 暂无评论