Skip to content

Intercepting API calls with NextJS Server Components #28469

Discussion options

You must be logged in to vote

So I have implemented a solution to this using mockttp.

Let's say you make server calls to some API, and you have the URL for that API stored in your .env file like so:

API_URL=https://jsonplaceholder.typicode.com

Then you make calls in the server components of your NextJS site like so:

const data = await fetch(process.env.API_URL, { /* options here... */ });

So when we run Cypress, we typically spin up an instance of our NextJS site at the same time, right? So what I did is I created a unique script to run my NextJS site in conjunction with Cypress testing, where I redefine the value for the API_URL env; e.g.:

{
  "scripts": {
    "start": "next start";
    "dev": "next dev";
+   "dev:cy…

Replies: 7 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Thor-Herman
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Thor-Herman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants