Next.js Discord

Discord Forum

Unable to load a script in public folder

Answered
Norwegian Forest Cat posted this in #help-forum
Open in Discord
Norwegian Forest CatOP
Hi,
I am struggling a lot whit script Loading on next js.

I simply added a file called test.js under public but it does not seem to be statically served.

If i go to http://localhost:3000/test.js i get redirected to my login page.

In my root layout i simply added

        <html lang='en'>
            <body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
                <Script src='/test.js' strategy='beforeInteractive' />
            </body>
        </html>


Every time i get:
Uncaught SyntaxError: Unexpected token '<'

If i go to the chrome developer tools, under sources, i can see that the test.js file that it loads is indeed an html file, and not my js file (that contains a simple console.log for testing purposes)

I tried:
1. Loading an external js file from a CDN. it works
2. Placing a sample jpg inside public and visualize it on http://localhost:3000/image.jpg. It works
3. Changing script load strategy
4. Clearing cache and going in incognito
5. Removing output: standalone from next config
6. Changing the file name various times

All of this while restarting the dev server every time

My ambient is:
Next js 15.2.3
Turbopack
Bun as package manager.

I already tried to use like pnpm or opt off from turbopack without success.

What can it be?
Thanks
Answered by Asian black bear
Getting redirected to the login page is indicative of your middleware intercepting it for auth, assuming you’re using one.
View full answer

2 Replies

Asian black bear
Getting redirected to the login page is indicative of your middleware intercepting it for auth, assuming you’re using one.
Answer
Norwegian Forest CatOP
You are right. Thanks a lot.
I lost too much time on this