sveltejs / sveltejs/kit

Add a boolean to mark prerenderable routes

Open
#14,493 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

  • #15627 by @teemingc — closed without merging
pkg:adapter-cloudflare
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

Hello! Inside my +layout.server.ts, I load a Cloudflare service binding stored in platform.env.backend to later make RPC calls from +page.ts in SSR contexts because we all know bindings cannot be accessed in universal loads directly. So this was the simple workaround.

The previous context aside to not confuse anyone, the actual problem is when the request comes from a prerendered route, say /, the following error is thrown inside my +layout.server.ts where I do the initialization:

[500] GET /
Error: Cannot access platform.env.backend in a prerenderable route

So I think it's necessary that we've a prerenderable boolean at least, say passed inside event.route so that you'd simply pre-check event.route.prerenderable to prevent accessing a service binding from there. Or even as a meta property such as import.meta.env.PRERENDERABLE if it makes more sense there.

Otherwise, SK devs just cannot avoid that error without 'hacks' such as the two workarounds below.

Describe the proposed solution

We need either something such as this:

event.route.prerenderable

Or this

import.meta.env.PRERENDERABLE
Alternatives considered

I got two workarounds.

First approach (Manual tracking):

// +layout.server.ts
import { load_backend_service } from "$lib/server";

const prerenderable_routes = [
    "/",
];

export async function load(event) {
    if (event.platform && !prerenderable_routes.includes(event.url.pathname)) {
        load_backend_service(event.platform); // event.platform.env.backend binding (Cloudflare Worker)
    }
}

Second approach (Ignore the error):

import { load_backend_service } from "$lib/server";

export async function load(event) {
    try {
        if (event.platform) {
            load_backend_service(event.platform); // event.platform.env.backend binding (Cloudflare Worker)
        }
    } catch { /* prerendering error ignored */ }
}

Second approach is nicer of course which is what I'm currently using, but I do think Svelte having a graceful condition would be a bit better than making a design practice out of ignoring errors in my opinion.

Importance

nice to have

Additional Information

I just think that it doesn't make sense for Svelte to know that a route is prerenderable while the developer is unaware of it and incapable of checking that truth.

But this is by no means a serious issue at all, and I also apologize if the flag already exists in some form despite attempting my best to search the docs beforehand.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the +layout.server.ts example and the event.route and event.platform behavior described in the issue, then inspect linked pull request #15627 for existing work. Define how a prerenderable route indicator should be exposed and verify that it lets server loads avoid accessing the Cloudflare binding during prerendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.