Giter VIP home page Giter VIP logo

Comments (10)

samselikoff avatar samselikoff commented on August 15, 2024 5

Update: vercel/next.js#10509 (comment)

from site.

ryanto avatar ryanto commented on August 15, 2024 4

Nice! Awesome to hear!

I think we should be able to get mirage released and the website updated with the nextjs example this week.

from site.

GriffinSauce avatar GriffinSauce commented on August 15, 2024

This would be great! Just spitballing: if you avoid SSR for pages it seems like Mirage could be initiated in _app.js and you'd be good to go.

from site.

GriffinSauce avatar GriffinSauce commented on August 15, 2024

I've attempted that but haven't been able to get it to work, perhaps because of the nextjs fetch polyfill? It's not really clear why Pretender isn't picking up on the requests.

Repo: https://github.com/GriffinSauce/nextjs-mirage-example

@timneutkens any chance you or a team member could take a look at this?

from site.

ryanto avatar ryanto commented on August 15, 2024

Thanks @GriffinSauce !

I just gave it a shot and think you're right about it having to do with the polyfill. The way that we (pretender) intercept fetch requests is by installing the whatwg-fetch polyfill, which changes window.fetch to use XMLHttpRequest, and then we rely on pretender to pickup those outgoing XMLHttpRequests.

It looks like in next we can't rely on whatwg-fetch, since it's getting replaced by another polyfill? I'm not too knowledgable about next, so correct me if I'm wrong here.

We'll need to investigate more, and probably come up with another way to catch these fetch requests in a next friendly way.

from site.

GriffinSauce avatar GriffinSauce commented on August 15, 2024

I've done a little digging. Seems like next.js already polyfills with whatwg:

https://github.com/zeit/next.js/blob/ecd628b7e0619da7677b57138a7dd59c1b581b59/packages/next/build/webpack-config.ts#L77

Imported here:

https://github.com/zeit/next.js/blob/04a7f1e85d34c72a3d45a05f4ca9d158ef1b5af0/packages/next/client/polyfills.js

And loaded here:

https://github.com/zeit/next.js/blob/2fe743581471279497b53ed692ff77c01be73e93/packages/next/pages/_document.tsx#L622

They make a specific effort to load this before anything else. Perhaps customising the webpack config to alias the modules could work but that greatly diminishes the DX and maintainability.

Didn't have time to try anything else yet, but it looks pretty tricky. :(

from site.

GriffinSauce avatar GriffinSauce commented on August 15, 2024

Just to let you know, I just patched in the new pretender version (in my example) via yarn resolution and it works!

I see you already have a PR here so I didn't make one. :)
miragejs/miragejs#361

Thank for the effort!

from site.

Saikat180891 avatar Saikat180891 commented on August 15, 2024

Even I am trying to add MirageJs to Next app and I am finding it difficult. If we can run miragejs as a seperate server then I think it will work fine @samselikoff

from site.

samselikoff avatar samselikoff commented on August 15, 2024

Can finally close this – all the changes have been made so Mirage works w/Next.js for client-side API requests out of the box!

https://miragejs.com/quickstarts/nextjs/develop-a-page/

from site.

GriffinSauce avatar GriffinSauce commented on August 15, 2024

πŸ™Œ Thanks Sam!

from site.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.