sveltejs / sveltejs/kit

event.locals & event.platform for universal load when running in SSR

Open
#13,343 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

load / layout
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

Hi, I recently ran into a problem with using static web apps azure adapter.

Each request to azure function (backend of the app) is accompanied with an invocation context, which holds methods (v3) or object (v4) to log. Adapter puts it to the event.platform, which is fine and expected.

With the .server.ts it's all pretty clear, the event.locals would hold the logger, which is bound to event.platform. But in the universal function during the SSR there is no way to access that logger (except singletons)

So if an error happens in the universal function in SSR mode, there is no way to report it (beside sentry, for example), console.log methods are ignored by the azure functions.

It would be nice to have the locals and platform available (as optionals) to the universal load in the SSR mode, if that makes sense.

Thank you very much in advance!

Describe the proposed solution

Optionals event.locals & event.platform when SSR is running.

Alternatives considered

TLDR: pain & singletons

hooks.server.ts

...
event.locals.logger = new Logger(...)
setSSRRequestLogger(requestId, logger); // These are flushed and deleted periodically e.g. each 5 secs
...

root +layout.server.ts

...
return {
 requestId,
correlationId,
}

root +layout.ts

if (browser) {
 return {
  some data
 }
} else {
 return {
  somedata,
  requestId: event.data.requestId,
  correlationId: event.data.correlationId
 }
}

child +layout.ts

if (browser) {
 create local logger/console
} else {
 await parentData()
 logger = singleton.getLogger(parentData.requestId)
 .... catch(error) {
   logger.report(error)
 }
}
Importance

would make my life easier

Additional Information

No response

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 Azure adapter’s handling of event.platform, then trace how event.locals and event.platform flow through hooks.server.ts, universal load functions, and the root and child +layout files during SSR. Confirm the intended optional values and the SSR behavior before checking that universal load can access them without relying on singletons.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.