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

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

Closed
StitiFatah opened this issue Dec 13, 2021 · 6 comments
Labels
Middleware Related to Next.js Middleware

Comments

@StitiFatah
Copy link

StitiFatah commented Dec 13, 2021

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 : vercel/examples#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
@StitiFatah StitiFatah added the bug Issue was opened via the bug report template. label Dec 13, 2021
@StitiFatah StitiFatah changed the title Are _middleware.ts not usable with getStaticProps + fallback true as well as with CSR only ? Unhandled Runtime Error, incompatible-href-as when using _middleware.tsx with getStaticProps + fallback true as well as when using CSR only. Dec 13, 2021
@StitiFatah StitiFatah changed the title Unhandled Runtime Error, incompatible-href-as when using _middleware.tsx with getStaticProps + fallback true as well as when using CSR only. "Unhandled Runtime Error, incompatible-href-as" when using _middleware.tsx with getStaticProps + fallback true as well as when using CSR only. Dec 13, 2021
@StitiFatah StitiFatah changed the title "Unhandled Runtime Error, incompatible-href-as" when using _middleware.tsx with getStaticProps + fallback true as well as when using CSR only. "Unhandled Runtime Error, incompatible-href-as" when using _middleware.ts with getStaticProps + fallback true as well as when using CSR only. Dec 13, 2021
@moinulmoin

This comment has been minimized.

@StitiFatah StitiFatah changed the title "Unhandled Runtime Error, incompatible-href-as" when using _middleware.ts with getStaticProps + fallback true as well as when using CSR only. "Unhandled Runtime Error, incompatible-href-as" when using _middleware.ts rewrites with getStaticProps + fallback true as well as when using CSR only. Dec 18, 2021
@Username2k1
Copy link

Facing the same issue, if we can't use middlewares rewrites this way why not specifying it in the docs ?

@balazsorban44 balazsorban44 added Middleware Related to Next.js Middleware kind: bug and removed bug Issue was opened via the bug report template. labels Dec 20, 2021
@StitiFatah
Copy link
Author

StitiFatah commented 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.

@albertpurnama
Copy link

albertpurnama commented Jan 10, 2022

@StitiFatah @Username2k1 Have you tried to use 12.0.8-canary-7?

This might be rehydration issue which is fixed in this PR #32534

cc @javivelasco @timneutkens

@StitiFatah
Copy link
Author

StitiFatah commented Jan 11, 2022

@StitiFatah @Username2k1 Have you tried to use 12.0.8-canary-7?

This might be rehydration issue which is fixed in this PR #32534

cc @javivelasco @timneutkens

@albertputrapurnama Thanks for pointing to this PR, I tried with 12.0.8-canary.20 and it now works with fallback:true and also with CSR.

I'm closing the issue as everything seems to be solved.

@balazsorban44
Copy link
Member

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.

@vercel vercel locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Middleware Related to Next.js Middleware
Projects
None yet
Development

No branches or pull requests

5 participants