Next.js Deployed on Vercel Now - Dynamic Links Return 'An Unexpected Error has Occurred'

12 replies
Last updated: May 9, 2020
Hello. I followed the Next.js tutorial, deployed on Now, the dynamic links renders
An unexpected error has occurred.
AI Update

Based on your description, you're encountering an "unexpected error" with dynamic links after deploying your Next.js app with Sanity to Vercel. This is almost certainly due to a breaking change in Next.js 15 regarding dynamic route parameters.

The Problem

In Next.js 15, the params prop in dynamic route pages is now a Promise instead of a plain object. If you're accessing params.slug (or any param) directly without awaiting it, your page will crash in production.

The Solution

You need to await the params before accessing any properties. Here's how to fix it:

❌ Old way (causes error):

export default function Page({ params }: { params: { slug: string } }) {
  const { slug } = params; // This breaks in Next.js 15
  // ...
}

✅ New way (correct):

export default async function Page({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params; // Await the params first
  // ...
}

Complete Working Example

Here's a full example with Sanity data fetching:

import { defineQuery } from "next-sanity";
import { client } from "@/sanity/client";

const query = defineQuery(
  `*[_type == "page" && slug.current == $slug][0]{title, content}`
);

export default async function Page({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;

  const data = await client.fetch(query, { slug });

  return (
    <div>
      <h1>{data.title}</h1>
      <div>{data.content}</div>
    </div>
  );
}

What Changed:

  1. Component is now async - Added async keyword
  2. Type updated - params is typed as Promise<{ slug: string }>
  3. Params are awaited - const { slug } = await params;

This applies to all dynamic routes in your app:

  • [slug]/page.tsx
  • [id]/page.tsx
  • [category]/[slug]/page.tsx
  • Any route with brackets []

After updating your dynamic route files with these changes, redeploy to Vercel and your pages should work correctly. This is documented in the Next.js App Router guide where it notes that Next.js 15 requires awaiting route params.

Here’s what I did: Cloned the repo for the tutorial: https://github.com/sanity-io/tutorial-sanity-blog-react-next
Followed the steps in the readme file. Including deployment to Now.
Currently deployed to: https://testbug.now.sh
I’ve not changed any of the code in the repo, other than following the instructions in the readme. I added some comments to the index page.
I tried including a hardcoded link to the page the dynamic link points to – that works.
Like I said, the blog article page shows
An unexpected error has occurred.
Refreshing the page renders the page fine.
Have you added https://testbug.now.sh to the CORS settings in manage.sanity.io ?
Let’s see. I ran:
sanity cors add <http://localhost:3000> --no-credentials
But of course I need to add the remote site too…
Yeah, try that. I think that will fix it
It does. Thanks very much!
Coolio!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?