-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[feat] expose handler to allow use in custom server #2051
[feat] expose handler to allow use in custom server #2051
Conversation
|
What if instead of // index.js
export const paths = {
assets: join(__dirname, '/assets'),
prerendered: join(__dirname, '/prerendered')
};
export const handler = (req, res, next) => {...}; That way you could either run the 'reference implementation' server-in-a-box — |
Yes, that sounds legit and possible. So also the 'reference implementation' would then use the fake server object to trick listen() from polka, just to make a plain node httpServer on its own. (I forgot to mention, that having your own node and/or express server using this approach would make #2048 obsolete as this could be achieved in 'userland'.) |
I like this approach very much as this would render most of the open issues asking for middleware's, server-side-events, websockets etc obsolete, as all of this could be easily achieved in userland with your custom server. All of that without compromising on sveltekit's (serverless) core and while giving enough freedom for future technologies. \o/ 💯 |
@benbender, I really like this approach hoping that it'll land very soon. At the same time, we need to understand the needs of many developers to receive the same experience when coding (dev) without resorting to other complexities (like proxies), complex and non stable hacks. Please, see my comment here: #887 (comment) |
I am able to split inside packages/adapter-node (files/ directory) into the two portions mentioned by @Rich-Harris (an index.js and a server.js), but I am struggeling what I need to change in the the later step, when running |
I think I'd override the Your own file could look pretty similar to the default, but would need a different file path to the app:
|
Btw, I'm on the Svelte Discord under this username if you want to ping me with any questions |
879d4bb
to
008396f
Compare
@benmccann I've managed to come up with my interpretation of @Rich-Harris comments somehow. Feel free to have a look and give feedback. I had some struggle with externalising the handler from the reference server. I also did not expose anything other than the handler/middleware itself from the whole polka/sirv world, yet, so no new API. I hope I didn't break existing API though. |
|
||
You can of course your favorite server framework to combine with other and/or custom middleware, e.g. [express](https://github.com/expressjs/expressjs.com). | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other questions the docs should answer: Where do you put this file? Is there some option you need to specify to make it use this vs the default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean. Using it this way is kind of advanced stuff for experienced node users. So I am not sure how to verbose to document. I'm not a native speaker, so feel free to come up with better documentations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if you want to create your own server using the code below does it matter what the file path or name is?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1b10a92
to
8d7c377
Compare
aeb0a82
to
0ea47e7
Compare
@matths I've rebased this a couple more times to keep it up-to-date with |
Very eagerly waiting for this feature.
|
@pranaypratyush, maybe. I do not know gitpkg. For my tests I have sveltekit checked out locally, build the node-adapter with // import adapter from '@sveltejs/adapter-node';
import adapter from './src/lib/adapter-node-request-handler/index.js'; |
Try using pnpm link for that, it also works for pretty much any package that you use it on. |
f58942c
to
26d9f1b
Compare
26d9f1b
to
4dcde29
Compare
If I understand correctly, we still need to build the sveltekit app before we can use custom server? (e.g., express, polka) What's the difference then between this or just having an external server? :/ |
@mirabledictu you now don't need two node servers, one for your other middleware and one to serve your sveltekit app. But please be fully aware, that @Rich-Harris prefers to hold off an escape hatch like this in favor of being portable and support serverless and other adapters beside plain node.
Originally posted by @Rich-Harris in #1563 (comment) So this PR is an escape hatch specifically for running sveltekit as one-of-many middlewares in another node, express, polka or whatever node app server. It's absolute valid to help the others to come up with a more sveltish way to solve the underlying problems / use cases. ;) |
Thanks for this, I've just been playing around with it and it works great. One issue, though: the client-side router isn't aware of any routes created via a custom Express server (obviously), so it shows a 404. Any thoughts on an approach to take here? I tried creating an |
This PR will let you add middleware in front of SvelteKit, which you cannot do with an external server. Also, the question of what's the difference between having one server or two servers seems like an obviously silly one. It's less processes to manage, you have a single build process, everything is in a single codebase without a need for shared libraries, etc. I'm not really sure what else you were looking for?
You can set |
@matths and @benmccann, thanks a lot for your work on this subject 👍 https://discord.com/channels/457912077277855764/943462063261511680 discibes in detail the question how you could pass information from an express session onto sveltekit when you use // src/server.js - here I use express middleware to read a users IP address before I hand over to svk using handler()
import { handler } from '../build/handler.js'
import express from 'express'
import session from 'express-session'
import parser from 'ua-parser-js'
const app = express()
if (user-from-outside-Europe) {
// a route that lives separately from the SvelteKit app
app.get('/non-svk-route', (request, response) => { response.end('non-svk route...') })
}
// let SvelteKit handle everything else, including serving prerendered pages and static assets
app.use(handler)
app.listen(3000, () => { console.log('hey...listening on port 3000') }) So how would one pass data from an express session onto sveltekit so it could be used with |
You can't right now, but it would be a very simple change to SvelteKit to enable it. There's a |
I stumbled across a problem similar to the ones in #334.
Currently the build process using adapter-node exports a Polka instance listening to a configurable host/port.
This way the SvelteKit app is the only requestHandler, so to say.
So my proposal is to export the app as a requestHandler instead.
A requestHander (
(req, res) => {}
) is very similar to an express like middleware ((req, res, next) => {}
), just missing thenext()
callback.This way you could combine the SvelteKit app with other requestHandlers and/or middleware.
Example
Provided using adapter-node from this branch, you could have the option
asRequestHandler
in your svelte.config.jsWhen calling
npm run-script build
in your project, you can no longer run your build usingnode build
.Instead you need to implement your own server (or express application) and import the build instead.
I haven't tested with express yet, but it should look similar to this
Tests
As the added code is mostly in files that are object of the build process, I haven't found a good example on how to come up with a test. I did some manual testing of the functionality of course.
Changesets
no changeset provided, yer.