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.

"Unhandled Runtime Error, incompatible-href-as" when using _middleware.ts rewrites with getStaticProps + fallback true as well as when using CSR only.

See original GitHub issue

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

17.1.0

What browser are you using?

Chromium, Brave, Firefox

What operating system are you using?

Archlinux

How are you deploying your application?

locally with yarn dev and also yarn start

Describe the Bug

I get Unhandled Runtime Error, incompatible-href-as when using _middleware.ts to do hostname rewrites, other details and screenshots in this issue : https://github.com/vercel/examples/issues/55 .

Screenshot_2021-12-13_18-47-59

The error happens when cloning this Vercel example repo https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites or just with a minimal example as shown below (I have no <Link/> or <a/> in my code)

The error comes under these conditions :

  • no getStaticProps
  • when fallback is true and trying to access a non generated at build path ( that’s what the screenshot is about ) ( it works well with fallback blocking )

Code

pages/_middleware.ts

import { NextRequest, NextResponse } from "next/server";

export default function middleware(req: NextRequest) {
  const { pathname } = req.nextUrl;
  // Get hostname (e.g. vercel.com, test.vercel.app, etc.)
  const hostname = req.headers.get("host");


  const currentHost = hostname?.replace(`.${process.env.ROOT_URL}`, "");

  console.log("current host", currentHost);

  if (pathname.startsWith(`/_sites`)) {
    return new Response(null, { status: 404 });
  }

  if (
    !pathname.includes(".") && // exclude all files in the public folder
    !pathname.startsWith("/api") // exclude all API routes
  ) {
    const rewrited_path = `/_sites/${currentHost}${pathname}`;
    console.log(`pushed to ${rewrited_path}`);
   
    return NextResponse.rewrite(rewrited_path);
  }
}

working when visiting /newpage (if the used subdomain path has been generated at build, if not it triggers the error) with getStaticProps :

pages/_sites/[site]/newpage.tsx


export default function NewPage({ domain }) {
  return <div> New Page {domain} </div>;
}


export async function getStaticProps(context) {
  const domain = context.params.site;

  return {
    props: {
      domain,
    },
  };
}

const mockDB = [
  {
    name: "Site 1",
    description: "Subdomain + custom domain",
    subdomain: "domain-1",
  },
  {
    name: "Site 2",
    description: "Subdomain only",
    subdomain: "new-domain",
  },
  {
    name: "Site 3",
    description: "Subdomain only",
    customDomain: "custom-domain.com",
  },
];


export async function getStaticPaths() {
  const site_paths = mockDB.map((elem) => {
    if (elem.customDomain) {
      return { params: { site: elem.customDomain } };
    } else {
      return { params: { site: elem.subdomain } };
    }
  });

  return {
    paths: site_paths,
    fallback: true,
  };
}

Not working when visiting /newpage, get the error

export default function NewPage() {
  return <div>NewPage</div>;
}

Expected Behavior

No error.

To Reproduce

Run the snippets of code below to check :

  • no getStaticProps
  • when fallback is true and trying to access a non generated at build path

Clone this repo from vercel examples https://github.com/vercel/examples/tree/main/edge-functions/hostname-rewrites to only check:

  • when fallback is true and trying to access a non generated at build path

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
StitiFatahcommented, Jan 3, 2022

Hi @moinulmoin. Have you personally found a solution ? Using fallback blocking isn’t a big deal but not being able to use CSR is kinda unfortunate.

0reactions
balazsorban44commented, Feb 10, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

`throw new Error('Failed to load static props')` when setting ...
After lot of searching and trial and error, I found out that the error was because of the exceptions throws inside getStaticProps ....
Read more >
Data Fetching: getStaticPaths - Next.js
Learn how to fetch data and generate static pages with `getStaticPaths`. ... If fallback is true , then the behavior of getStaticProps changes...
Read more >
Next.js 9.3+ Data Fetching Explained - YouTube
In this video we are going to learn how to work with the getServerSideProps(), getStaticProps (), and getStaticPaths() methods for static ...
Read more >
Next.js Tutorial - Part 8 | Static Site Generation (SSG) - YouTube
In this video, we will build an e-commerce website and learn how to use getStaticProps and getStaticPaths in Next.js to statically generate ...
Read more >
getstaticprops not passing props - You.com | The AI Search ...
js where I form paths using getStaticPaths, fetch data from Firebase inside getStaticProps, and pass a props array to my component. But when...
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