# NextJS deploy cannot force 404

**URL:** <https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055>\
**Category:** Support\
**Tags:** redirects, nextjs\
**Created:** [October 25, 2022, 1:00pm UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055 "2022-10-25T13:00:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![niels\_bankai](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/niels_bankai/32/36126_2.png) [@niels\_bankai](https://answers.netlify.com/u/niels_bankai)\
**Post date:** [October 25, 2022, 1:00pm UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/1 "2022-10-25T13:00:36Z")

</div>

Hi there,

I’m having a problem with my NextJS site. For some reason, I cannot force a 404 page to be rendered.

I have a page `pages/[...slug].tsx`. In it’s `getStaticProps` function, I do this:

```auto

if (isProtected(context)) {
  console.log(`Page "${slug}" is protected`)
  return {
    notFound: true,
  }
}

console.log(`Page "${slug}" is NOT protected`)

// Normal operations resume here...

```

Basically, during build time, some environment variable is checked and a `notFound: true` is returned which forces a 404. During build time in Netlify, I see this in the logs:

```auto
2:27:05 PM: info - Collecting page data...
2:27:07 PM: info - Generating static pages (0/24)
2:27:09 PM: info - Generating static pages (6/24)
2:27:09 PM: Page "about" is protected
2:27:09 PM: Page "contact" is protected
2:27:09 PM: info - Generating static pages (12/24)
2:27:09 PM: Page "" is protected
2:27:09 PM: Page "services/transformation-sprint" is protected
2:27:09 PM: Page "services/design-sprint" is protected
2:27:09 PM: info - Generating static pages (18/24)
2:27:09 PM: Page "services" is protected
2:27:09 PM: info - Generating static pages (24/24)
2:27:10 PM: info - Finalizing page optimization...

```

This is correct. All of those pages should return a 404 on build time. However, if I visit those url’s on Netlify after the deploy is completed, those pages show up. Locally, this works (running `yarn build` and `yarn start` will force the 404 pages).

Looking a bit further into the deploy logs, I find this redirect:

```auto
2:27:10 PM: {
2:27:10 PM: from: '/:slug/*',
2:27:10 PM: to: '/.netlify/builders/___netlify-odb-handler',
2:27:10 PM: status: 200,
2:27:10 PM: force: false
2:27:10 PM: },

```

This does’t seem right to me. This URL should redirect to a 404 page?

Site is located at [https://develop–bankai-website-v4.netlify.app](https://bankai-website-v4.netlify.app/).

Hope you can help me.

Kind regards, Niels.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [October 27, 2022, 4:14pm UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/4 "2022-10-27T16:14:18Z")

</div>

Hey @niels_bankai,

Is there a specific use case that you’re trying to achieve? I’m asking because if you always wish to force a page to 404, why not simply use Netlify Redirects, like:

```auto
/about /404.html 404!

```

But to solve your actual issue, I can see that in your function logs: [Netlify App](https://app.netlify.com/sites/bankai-website-v4/functions/___netlify-odb-handler?scope=branch:develop)

 ![image](https://global.discourse-cdn.com/netlify/original/3X/6/2/623034f6e93aba8da008e508b62a1577661c5368.png)

We’re not seeing that as same as your build during the runtime.

Could you share your `isProtected()` function? How does that determine what you need exactly?

---

<div class="post-metadata">

**Author:** ![niels\_bankai](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/niels_bankai/32/36126_2.png) [@niels\_bankai](https://answers.netlify.com/u/niels_bankai)\
**Post date:** [October 28, 2022, 12:01pm UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/5 "2022-10-28T12:01:30Z")

</div>

I am trying to implement my own access control to the site, using an environment variable. This site is driven by a headless CMS (Storyblok), which has a visual preview editor (an iframe which loads the Netlify site and injects data into the NextJS application). So, I need the site to be accessible to users that are logged into the CMS. Regular visitors should not have access to the site. Setting a password in Netlify access control is not an option.

The setup is as following:

Production branch → accessible for both visitors and editors logged into the CMS  
Development branch (where the problem is) → unaccessible for visitors, accessible for editors logged into the CMS

The isProtected function looks like this:

```auto
export const isProtected = (
  context: GetStaticPropsContext<ParsedUrlQuery, PreviewData>
) => !context.preview && parseBoolean(process.env.PROTECTED)

```

The `context.preview` check is NextJS’ preview mode ([Advanced Features: Preview Mode | Next.js](https://nextjs.org/docs/advanced-features/preview-mode)). The `parseBoolean` function just converts a string to an actual boolean, nothing special. The `PROTECTED` env variable is set in `netlify.toml`:

```auto
[context.develop.environment]
  NEXT_PUBLIC_BASE_URL = "https://develop--bankai-website-v4.netlify.app"
  PROTECTED = "true"

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [October 30, 2022, 1:21pm UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/6 "2022-10-30T13:21:43Z")

</div>

> [@niels\_bankai](#):
>
> The `context.preview` check is NextJS’ preview mode ([Advanced Features: Preview Mode | Next.js](https://nextjs.org/docs/advanced-features/preview-mode)). The `parseBoolean` function just converts a string to an actual boolean, nothing special. The `PROTECTED` env variable is set in `netlify.toml`:

`netlify.toml` environment variables are not usable during the runtime, they’re only accessible during the build time.

You need to set a variable in the UI.

---

<div class="post-metadata">

**Author:** ![niels\_bankai](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/niels_bankai/32/36126_2.png) [@niels\_bankai](https://answers.netlify.com/u/niels_bankai)\
**Post date:** [October 31, 2022, 8:17am UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/7 "2022-10-31T08:17:37Z")

</div>

I can’t do that, because this variable only needs to apply to a single branch. I want the main branch to be fully accessible, so this variable needs to be `false` for that branch, yet it needs to be `true` for the develop branch. Since Netlify doesn’t have an option to set separate variables per branch in the UI, this is the only way I can make a distinction between those two branches.

However, your reply did lead me to the solution. NextJS strips variables which are not prefixed with `NEXT_PUBLIC_` upon build time. In theory, that shouldn’t matter, since I am calling this `isProtected` function inside `getStaticProps`, thus I’m using SSG to create the 404 pages at build time. In practice, this does have an effect. I don’t know why, but if I prefix the env variable with `NEXT_PUBLIC_`, it does work. I think this has something to do with how Netlify creates NextJS handler functions? Again, I’m not sure, but at least it now works.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![fool](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/fool/32/37_2.png) [@fool](https://answers.netlify.com/u/fool)\
**Post date:** [October 31, 2022, 11:44pm UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/8 "2022-10-31T23:44:14Z")

</div>

Not sure why that flow was needed, but glad to hear you found something that works for you!

This (experimental for a while longer, but we use it in production already and it works well) feature does let you set branch-specific variables that can be used with functions: [Environment variables overview | Netlify Docs](https://docs.netlify.com/netlify-labs/experimental-features/environment-variables/get-started/)

---

<div class="post-metadata">

**Author:** ![niels\_bankai](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/niels_bankai/32/36126_2.png) [@niels\_bankai](https://answers.netlify.com/u/niels_bankai)\
**Post date:** [November 9, 2022, 9:07am UTC](https://answers.netlify.com/t/nextjs-deploy-cannot-force-404/78055/9 "2022-11-09T09:07:29Z")

</div>

Update: I’ve tried switching to the new environment variables setup in the UI and I no longer have to prefix my ENV variables with NEXT\_PUBLIC\_. I’ll be sticking to this. Thanks for the help!
