question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Runtime error on Next.js with mirage

See original GitHub issue

Hello, I’m trying to setup miragejs with my Next.js Project. But when I setup mirage server with <Link> in Next.js it shows following error.

import { Server } from "miragejs"

new Server({
  routes() {
    this.get("/api/movies", () => ({
      movies: [
        { id: 1, name: "Inception", year: 2010 },
        { id: 2, name: "Interstellar", year: 2014 },
        { id: 3, name: "Dunkirk", year: 2017 },
      ],
    }))
  },
})

export default function Home() {
  return (
    <div>
      <Link href="/posts">
        <a>Posts</a>
      </Link>
      <p>index page</p>
    </div>
  )
}
Screen Shot 2020-10-03 at 21 03 24

Does anyone knows about this?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

25reactions
samselikoffcommented, Oct 21, 2020

Gotcha - thanks for digging into this @arsnl!

The passthrough API is indeed weird in that it inherits any previously set namespace, but you can actually rest it back to the empty string before calling passthrough() if you want it to work on the root of the current domain, like this:

createServer({
  routes() {
    this.namespace = 'api';

    // handles GET requests to /api/movies
    this.get("/movies", () => ({
      movies: [
        { id: 1, name: "Inception", year: 2010 },
        { id: 2, name: "Interstellar", year: 2014 },
        { id: 3, name: "Dunkirk", year: 2017 },
      ],
    }))

    // resets the namespace to the root
    this.namespace = "" // or this.namespace = "/"
    this.passthrough() // now this will pass through everything not handled to the current domain (e.g. localhost:3000)
  },
})

I think the “real” solution here is to improve the passthrough/namespace API so it’s clear how the prefix is applied.

7reactions
arsnlcommented, Oct 19, 2020

Depending of how your routes are defined in your mock server, you gonna have to set the passthrough differently.

If don’t use the namespace, you can use the global passthrough() at the end of all your routes or a specific passthrough("/_next/static/development/_devPagesManifest.json").

If you are using the namespace, you gonna have to use the solution proposed by @ynotdraw since I think the call for /_next/static/development/_devPagesManifest.json gonna be resolved to api/_next/static/development/_devPagesManifest.json (if the namespace is api). So, the call for GET /_next/static/development/_devPagesManifest.json will not be filtered by the passthrough.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Develop a Next.js page with Mirage
Mock out client-side API requests directly within a page of your Next.js app so you can continue local development without any backend services....
Read more >
Next.js Unhandled Runtime Error "Failed to load script
I'm seeing the same thing. I tried adding the URL in the 'as' attribute of the Link component, but that did not help....
Read more >
Unhandled Runtime Error There was an Error While Hydrating ...
UnhandledRuntimeError #react-hydration-errordocshttps:// nextjs.org/docs/messages/react-hydration-errorlike and subscribesupport this channel ...
Read more >
20 Reasons Why You Need To Stop Being Highly Obsessed ...
JavaScript's strict mode isn't always the profounded choice ... When I enter strict mode and run the code, a runtime error will occur:....
Read more >
Work journal - Sam Selikoff
Published "Auth-based route guards in Next.js and debugging in production", ... React will delegate to them in the event of a render-time error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found