Sapper: Support Koa or koa-connect

Created on 23 Jan 2019  路  9Comments  路  Source: sveltejs/sapper

import sirv from 'sirv';
import compression from 'compression';
import * as sapper from '../__sapper__/server.js';

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';

const Koa = require('koa');
const c2k = require('koa-connect');
const app = new Koa()

app.use(c2k(compression({ threshold: 0 })));
app.use(c2k(sirv('static', { dev })));
app.use(c2k(sapper.middleware()));

app.listen(PORT);

pending clarification proposal

Most helpful comment

@focus-at I don't know anything about Koa I'm afraid, but possibly if you have non-sapper routes in your server, you need to ignore them in the sapper middleware:

sapper.middleware({
      ignore: [
        uri => uri.startsWith('/some-non-sapper-route')
      ]
})

All 9 comments

this works for http://localhost:3000/blog or any routes,
but http://localhost:3000/ trows Internal Server Error

@focus-at I don't know anything about Koa I'm afraid, but possibly if you have non-sapper routes in your server, you need to ignore them in the sapper middleware:

sapper.middleware({
      ignore: [
        uri => uri.startsWith('/some-non-sapper-route')
      ]
})

@antony I was looking for this 馃槃 thanks!

@focus-at I don't know anything about Koa I'm afraid, but possibly if you have non-sapper routes in your server, you need to ignore them in the sapper middleware:

sapper.middleware({
      ignore: [
        uri => uri.startsWith('/some-non-sapper-route')
      ]
})

ty for example Antony, as you can see i dont have non-sapper routes now, but next time im add them.
the problem is that all the sapper routes except "/" work

@Rich-Harris Any reason why / route does not work?

This issue is not only specific to Koa, but also applies to other frameworks like https://github.com/jkyberneees/ana

Here's what worked for me in the sapper-template app. Have not tested beyond that.

import Koa from "koa";
import connect from "koa-connect";
import koaStatic from "koa-static";
import * as sapper from '../__sapper__/server.js';

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';

const app = new Koa();

app.use(koaStatic("static"));
app.use(connect((req, res, next) => {
  req.originalUrl = req.url;
  sapper.middleware()(req, res, next);
}));

app.listen(PORT);
import Koa from "koa";
import sirv from "sirv";
import * as sapper from '@sapper/server';

const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === 'development';

const app = new Koa();

app.context.respond = false;

app.use(async (ctx, next) => {
    sirv('static', { dev })(ctx.request, ctx.res, next);
});

app.use(async (ctx, next) => {
    sapper.middleware({
        session: (req, res) => {
            return { user: 123 };
        }
    })(ctx.request, ctx.res, next);
});

app.listen(PORT);

app.context.respond = false (but i'm not sure what is the best idea) - from koajs.com

ctx.respond
To bypass Koa's built-in response handling, you may explicitly set ctx.respond = false;. Use this if you want to write to the raw res object instead of letting Koa handle the response for you.

Note that using this is not supported by Koa. This may break intended functionality of Koa middleware and Koa itself. Using this property is considered a hack and is only a convenience to those wishing to use traditional fn(req, res) functions and middleware within Koa.

AND

path /client/* - 404

The previous comment is the best workaround to do this right now.

There may be value in opening a new issue to make Sapper middleware less greedy, but right now, wrapping it allows you to have non-sapper routes on the same handler.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rich-Harris picture Rich-Harris  路  3Comments

antony picture antony  路  3Comments

Rich-Harris picture Rich-Harris  路  3Comments

BMorearty picture BMorearty  路  3Comments

nolanlawson picture nolanlawson  路  4Comments