Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to run code on server start #208

Closed
benmccann opened this issue Dec 1, 2020 · 4 comments
Closed

Ability to run code on server start #208

benmccann opened this issue Dec 1, 2020 · 4 comments

Comments

@benmccann
Copy link
Member

@aewing on Discord couldn't figure out how to do database setup. Was requesting:

Some mechanism to intercept the request before it goes to snowpack.handleRequest or some startup function I can introduce in my adapter config that runs before createServer and can populate context...

I ignore most of the help requests, but this one seemed like it might be a feature gap rather than a usage / documentation issue. Feel free to close if this use case is already supported in some way

@Rich-Harris
Copy link
Member

Depends on the specifics. We already have this...

// src/setup/index.js
export function prepare(headers) {
  // this function runs before every page/endpoint request
  return {
    context: {...}, // this object is passed to endpoints, and used to derive session
    headers: {...} // this object is mixed in with response headers
  };
}

export function getSession(context) {
  return {
    user: context.user && {
      name: user.name
    }
  };
}

...but without knowing more about the problem it's hard to know if that's the solution

@benmccann
Copy link
Member Author

benmccann commented Dec 1, 2020

I followed up with the user. It seems they were conflating a couple different issues. Their main complaint was around Snowpack's bundling of Node dependencies. I've opened a new issue for that (#210)

But anyway, it does seem like it'd be a nice API to support one-time setup stuff somewhere. You can probably work around it using the per-request method and setting a global flag to have it executed only on the first call, but it's a little messy to do it that way and if it takes a long time you might have requests that are hanging or failing whereas if it were done separately it might be able to be better handled

@Rich-Harris
Copy link
Member

Still not totally sure what the suggestion is — what does 'one-time setup' mean in the context of a serverless deployment (for example)?

@benmccann
Copy link
Member Author

Yeah, I guess it wouldn't mean much in that context, which is an interesting call out. I'll close this for now and let that thought bounce around in my head for awhile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants