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

Added support for: reject status code and message; context.statusCode. #397

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mrauhu
Copy link

@mrauhu mrauhu commented Apr 21, 2020

Greetings, Guillaume @Akryum.

In this pull request I added support for:

  • reject code set status code and message appends to body:
    // no matched routes
    if (!matchedComponents.length) {
      reject({ code: 404, message: 'Not found' })
    }
  • context.statusCode and example of usage with wildcard * path for creating custom 404 error.

Best wishes,
Sergey.

P.S. This pull request is copy of #390, but from different branch (not master).

…ge`:

* `message` is now setting HTTP status code, 500 by default;
* `message` appends to body, 'Server Error' by default.
@chris-visser
Copy link
Contributor

@mrauhu It might be better to add machine readable strings. A bit like how the Meteor guide describes the error field: https://guide.meteor.com/methods.html#throwing-errors. This way we can apply translations to them and it follows a similar structure to how Apollo's resolvers work: https://www.apollographql.com/docs/apollo-server/data/errors/

@mrauhu
Copy link
Author

mrauhu commented Jun 6, 2020

It might be better to add machine readable strings.

Purpose of this changes is make a human readable Server-Side Rendered (SSR) 404 error page with a machine readable 404 Not found HTTP status code.

@chris-visser please, read carefully:
Current implementation of the vue-ssr have the bug, that breaks your SEO dramatically, it's return the 200 OK HTTP status code for not found pages.

Right now, the no matched routes example code from vue-ssr README.md not working, and return 200 OK to web crawlers (scrapers) as Googlebot, Bingbot, Baiduspider or YandexBot:

// no matched routes
if (!matchedComponents.length) {
reject({ code: 404 })
}

This pull-request fix the bug and make code cross-platform between the Vue SSR guide approach and Vue+Meteor SSR.

Basically, you can copy code from your Vue.js or Nuxt.js application to Vue+Meteor and it's works.

As example, you may return reject({ code: 404 }) as described in:
https://ssr.vuejs.org/guide/routing.html#routing-with-vue-router

Please, see the code from the Vue SSR guide:

Now we need to implement the server-side routing logic in entry-server.js:

    // wait until router has resolved possible async components and hooks
    router.onReady(() => {
      const matchedComponents = router.getMatchedComponents()
      // no matched routes, reject with 404
      if (!matchedComponents.length) {
        return reject({ code: 404 })
      }

      // the Promise should resolve to the app instance so it can be rendered
      resolve(app)
    }, reject)

A bit like how the Meteor guide describes the error field
This way we can apply translations to them and it follows a similar structure to how Apollo's resolvers work

It's not about Meteor or Apollo errors.

Optional the reject({ message: 'Custom error message'}) attribute needed for replace the default error message Server error that hardcoded in:

function writeServerError (sink) {
sink.appendToBody('Server Error')
}

Thank you for attention,
Sergey.

@chris-visser
Copy link
Contributor

Oh lol. Turned out I'd misread your example. You are showing it indeed as part of how devs could use it. Actually really nice this! 🙂

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

Successfully merging this pull request may close these issues.

None yet

2 participants