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

Okay to run code after a next() call in a route handler? #554

Open
tiffz opened this issue Jan 25, 2020 · 1 comment
Open

Okay to run code after a next() call in a route handler? #554

tiffz opened this issue Jan 25, 2020 · 1 comment

Comments

@tiffz
Copy link

tiffz commented Jan 25, 2020

Hello!

Our team uses page.js for routing in a SPA we maintain. A debate recently came up as we tried to figure out the best way to add a handler that ran after every page load for our app.

One idea that came up was the idea of having a '*' route handler that ran before all other routes and ran some functionality after the next() call.

ie:

page('*', universalRouteHandler);

universalRouteHandler(ctx, next) {
  doStuffBeforeRouteHandlers();
  next();
  doStuffAfterRouteHandlers();
}

This solution worked well for us because it allowed us to execute some functionality before specific route handler logic and other functionality afterwards without having to create multiple '*' route handlers.

However, a teammate noticed that none of page.js's documented examples ran logic in a function after a next() call and feared that what we had come up with was a hack.

So my question is essentially: is running logic after a next() call in a handler an acceptable way to specify an order for routing behavior?

Is it a reasonable assumption that logic after a next() call would always run only when other route handlers have executed or is coding in this way depending too much on implementation details of how page.js runs route handlers? ie: if it's possible that page.js could one day change its implementation to use async code in next().

(In our app, we originally started with a '*' route handler that was registered after all other routes, but we wanted to move away from this because it made next() handling complex for our use case.)

@coyotte508
Copy link

It should be fine if there's no async calls in the next handlers.

Too bad there's not a koa-like version of page, you could just do:

async function universalRouteHandler(ctx, next) {
  doStuffBeforeRouteHandlers();
  await next();
  doStuffAfterRouteHandlers();
}

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