I am using NextJS 14 app router and I can’t go back because site is in prod.
What I am trying to do is when fetch user is called i update route in cookie (route coming from server) at first everything works fine, even routes were protected, but as soon as I implement this method I get this error
app-pages-internals.js:1 Uncaught SyntaxError: Unexpected token '<' (at app-pages-internals.js:1:1)
page.js:1 Uncaught SyntaxError: Unexpected token '<' (at page.js:1:1)
Understand this error
webpack.js?v=1725952128143:1 Uncaught SyntaxError: Unexpected token '<' (at webpack.js?v=1725952128143:1:1)
Understand this error
main-app.js?v=1725952128143:1 Uncaught SyntaxError: Unexpected token '<' (at main-app.js?v=1725952128143:1:1)
Understand this error
layout.js:1 Uncaught SyntaxError: Unexpected token '<' (at layout.js:1:1)
My middleware file
import { NextResponse } from 'next/server'
const PROTECTED_ROUTES = [
'/clips',
'/find-clips',
'/setting/basic-info',
'/setting/user-plan',
'/contact-us',
]
const SUBSCRIPTION_REQUIRED_ROUTES = [
'/find-clips',
'/clips',
]
const UNAUTHENTICATED_ONLY_ROUTES = [
'/login',
'/register',
'/forgot-password',
'/forgot-email',
]
function isAuthenticated(request) {
return !!request.cookies.get('access_token')
}
function isSubscribed(request) {
return request.cookies.get('sub-token')?.value === 'true'
}
function hasUuid(request) {
return !!request.cookies.get('uuid')
}
function redirectTo(path, request) {
console.log(`Redirecting to: ${path}`)
return NextResponse.redirect(new URL(path, request.url))
}
export function middleware(request) {
const { pathname } = request.nextUrl
const profileRoute = request.cookies.get('profile-route')?.value
const userDataFetched = request.cookies.get('user-data-fetched')?.value === 'true'
console.log(`Current pathname: ${pathname}`)
console.log(`Profile route: ${profileRoute}`)
console.log(`User data fetched: ${userDataFetched}`)
// If user data hasn't been fetched, allow the request to proceed
if (!userDataFetched && isAuthenticated(request)) {
return NextResponse.next()
}
if (!isAuthenticated(request)) {
if (PROTECTED_ROUTES.some(route => pathname.startsWith(route))) {
return redirectTo('/login', request)
}
if (pathname.startsWith('/update-phone') || pathname.startsWith('/verify-phone')) {
return redirectTo('/', request)
}
if (!hasUuid(request) && pathname.startsWith('/confirm-registration')) {
return redirectTo('/', request)
}
} else {
if (profileRoute && pathname !== profileRoute && !pathname.startsWith('/api/')) {
return redirectTo(profileRoute, request)
}
if (!isSubscribed(request) && SUBSCRIPTION_REQUIRED_ROUTES.some(route => pathname.startsWith(route))) {
return redirectTo('/price-plan', request)
}
if (UNAUTHENTICATED_ONLY_ROUTES.some(route => pathname.startsWith(route))) {
return redirectTo('/', request)
}
}
return NextResponse.next()
}
export const config = {
matcher: '/:path*',
}
and my fetch user
import Cookies from "js-cookie";
import { requestWithAuth } from "./apiService";
export const fetchUserData = async (router, showToaster, dispatch, saveUser) => {
try {
if (!localStorage.getItem('login_email')) {
return
}
const email = localStorage.getItem('login_email')
const response = await requestWithAuth('GET', `/users/get-user`, {}, router, showToaster);
if (response) {
localStorage.setItem('logined_uuid', response.uuid)
localStorage.setItem('allowYoutube', response.allowYoutube)
localStorage.setItem('allowTwitch', response.allowTwitch)
localStorage.setItem('allowDownload', response.allowDownload)
Cookies.set('sub-token', response.is_active)
Cookies.set('profile-route', response.profile_status);
// Set a flag to indicate user data has been fetched
Cookies.set('user-data-fetched', 'true', { expires: 1 });
if (saveUser) {
dispatch(saveUser(response))
}
return response
}
return response
} catch (error) {
console.error('Error fetching user data:', error);
}
};
I tried other methods but I could find solution according to gpt and and median. My server is sending HTML not JS (I verified it, in response is embedded HTML).
I redirect to page that I want but cause css issue and unusable page