question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose a way to inject a start script into adapter-node

See original GitHub issue

Is your feature request related to a problem? Please describe.

I’m tying to migrate from Sapper to SvelteKit. I’m aware of the discussion in #334 and hooks seem to solve that.

I don’t think there is currently a way to have code that runs on startup with the Node adapter. There are things that need to happen once and not for every request. In Sapper my server.js looked like this:

import sirv from 'sirv';
import express from 'express';
import compression from 'compression';
import bodyParser from 'body-parser';
import * as sapper from '@sapper/server';
import { Model } from 'objection';
import knex from './lib/knex.js';
import env from './lib/env.js';

Model.knex(knex); // <==============================================

const app = express();

app.use(compression({ threshold: 0 }));
app.use(sirv('static', { dev: env.isDev }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(sapper.middleware());

(async () => {
  await knex.migrate.latest({  // <==============================================
    directory: './src/migrations',
  });

  app.listen(env.PORT, (err) => {
    if (err) {
      console.error(err);
    }
  });
})();

The first line I’ve marked is something that would probably not hurt if done in every hook (setting up my Objection models with the knex instance).

The second line I’ve marked is running the database migration. In a serverfull environment this is a rather common occurrence. I want to run the migration right before starting the server so that I’m in a consistent state between database and application code. Even if this is scaled across multiple instances it works, since the second time the migrations run it’s a NOOP. I honestly have no idea how people do that in a serverless environment with a guarantee that no code is run that expects an outdated schema resulting in possibly undefined behavior.

Describe the solution you’d like

A way to run setup code before the server starts. Maybe an async function setup() {} that is awaited before the server starts? But that can’t work with every adapter.

Describe alternatives you’ve considered

I guess some will argue I should have a second API server separated from SvelteKit and whatnot. But I want less complexity, not more.

How important is this feature to you?

I cannot migrate to SvelteKit, unless I’m missing something.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:10
  • Comments:46 (5 by maintainers)

github_iconTop GitHub Comments

16reactions
kylepaulsencommented, Jul 10, 2022

I’m confused why this isn’t a more important issue. There should be an easy way to call code on app startup both in dev mode and when built. Relying on a first request to do initialization doesn’t make any sense.

12reactions
ValeriaVGcommented, Sep 12, 2021

I use a hack-around for my SvelteKit TypeScript Node project:

// Create a promise, therefore start execution
const setup = doSomeAsyncSetup().catch(err=>{
  console.error(err)
  // Exit the app if setup has failed
  process.exit(-1)
})

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, resolve }) {
   // Ensure that the promise is resolved before the first request
   // It'll stay resolved for the time being
    await setup;
    const response = await resolve(request);
    return response
}

Here’s how it works:

  • App loads hooks and starts executing the promise
  • Until setup is complete all the requests are waiting
  • If the setup fails - the app exits with an error
  • Once setup is complete every request just jumps over await setup as it has already been resolved

Of course, having a setup hook would be cleaner and more straightforward than this. 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to inject local script with require statements into ...
You can use the page.exposeFunction to expose a function from the Node.js environment to the page itself. To quote the docs: The method...
Read more >
Adapters • Docs • SvelteKit
See the adapter-auto README for information on adding support for new environments. ... To create a simple Node server, install the @sveltejs/adapter-node ......
Read more >
All about Adapters - How to Deploy in SvelteKit - YouTube
Steph Dietz will tell us all about adapters and teach us how to deploy a SvelteKit application.
Read more >
Learn How SvelteKit Works As a Framework - Joy of Code
Kit isn't built on top of Svelte but it's a backend web framework where Svelte ... to package JavaScript code for reuse and...
Read more >
[Tutorial] How to build a SvelteKit Docker image to run ... - Reddit
Build a SvelteKit Docker image with adapter-node: npm init ... You need to add some code for that to happen (for example in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found