Faulty edge function triggers a download instead of rendering website

I’m running a Gatsby website that’s deployed on Netlify, and I’m running into an issue where opening the website in Incognito mode triggers a file download instead of rendering the website. Strangely, the website works as expected in a normal browsing session, and I haven’t made any recent updates to the Gatsby site.

Has anyone else experienced this issue or have any suggestions for how I might troubleshoot it?

https://s-aga.netlify.app/ (using this link also triggers a download)

@zamson It always triggers a download for me, regardless of it being a normal or incognito window.

So it may be that you currently have a cached response when not in incognito, or that the request is being handled by a function that is checking cookies (which wouldn’t be there in an incognito session).

Thanks for checking. I do have a Netlify function for checking the visitor country cookie but it has not been a problem in the past. Will try to remove it and see if the problem persists.

// netlify/edge-functions/country.tsx
import type { Context } from "https://edge.netlify.com";

export default async (request: Request, context: Context) => {  
  // Set a cookie
  if (!context.cookies.get('country')) { 
      name: "country",
      value: context.geo.country?.code || '',
      path: '/',
      sameSite: 'None',
      secure: true

This is what I see using Firefox

It is trying to render text/plain

Safari simply tries to download the file.

Thanks for helping out :slight_smile:

I removed the edge function and now the website is rendering properly. It was working fine before. The function was simple - it checked and set the country cookie based on Netlify’s geo-location features.

Will have to revise my code, something might have changed on Netlify’s side

Okay, problem seems to be fixed after revising the code of the edge-function and also changing the file extension from .tsx to .ts.

Thanks again for your swift help pointing me in the right direction :pray:t4:

1 Like

My edge function was missing a return function. I’m not sure why this was needed, some examples on the netlify edge github does not have a return functions and I did not have one before either.

Just returned a rewrite to the request URL and now the page loads again instead of downloading a file. What’s the best practice here?

Here is my working code:

If I remove the return statement, the website will trigger a download.

Hi @zamson Thanks for letting us know your problem was resolved! :wave:t6: