Skip to content

A way to validate things on server initialization #6147

@Zamiell

Description

@Zamiell

Describe the problem

Related to #1538.

In other server solutions, a common pattern is check for the presence of necessary environment variables on startup, for the purposes of showing a friendly error message.

For example:

const { DATABASE_URI } = process.env;

if (DATABASE_URI === undefined || DATABASE_URI === "") {
  console.error(
    'The "DATABASE_URI" environment variable is not set. This is necessary in order for the server to function.',
  );
  console.error(
    'If you are deploying to Node, did you forget to copy the ".env.example" file to ".env" and fill in the values?',
  );
  console.error(
    "If you are deploying to Vercel, did you forget to add this environment variable to your project settings page?",
  );
  process.exit(1);
}

Doing this kind of thing prevents end-users from having to troubleshoot more-complicated run-time errors. ("Why is the database logic failing? Is the app that I just downloaded bugged?")

In #1538, Rich-Harris recommends using "hooks.js", but this doesn't solve the problem, as the logic in that file will only be executed when a user actually surfs to the page, rather than on server initialization.

Describe the proposed solution

SvelteKit should expose an idiomatic way to perform server-start validation of this manner.

This would also an appropriate place to e.g. initiate necessary database connections, so that the app can e.g. fail early if the database password is wrong (rather than when the first user actually tries to use your product).

Importance

nice to have

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions