bfcache error in Nextjs v13 App Router
Unanswered
Egyptian Mau posted this in #help-forum
Egyptian MauOP
Many navigation are performed by going back to a previous page, or forwards again. The back/forward cache (bfcache) can speed up these return navigations.
can anyone help why this is not working, this error is coming in Lighthouse report
can anyone help why this is not working, this error is coming in Lighthouse report
38 Replies
Egyptian MauOP
can any one help me with this, that why this issue is there in Nextjs v13 App Router?
It only works in Production mode (with
next build && next start). In development mode, your app isn't optimizedEgyptian MauOP
I am doing this only when running production mode with
npm run build and then npm run startnot in dev mode
and I am getting above issue in Production Mode, not in dev mode ---- sorry to mention this
Can you reproduce the problem in CodeSandbox? I tested in my own projects in App Router, none of them have this problem
Egyptian MauOP
does it have anything with
import {usePathname} from 'next/navigation' that's the one I am using to show the active Page Link in Navbar in Layout fileYes
Egyptian MauOP
like I have a seperate folder for folder where I've navbar + footer code, and then wrapping it in
/app/layout.tsx file and in that navbar code I am using import {usePathname} from 'next/navigation' and then pathname === arrayName.pathName, so here it can be a problematic?@fuma
it shouldn't, because I'm doing the same in my project and it works perfectly
@fuma it shouldn't, because I'm doing the same in my project and it works perfectly
Egyptian MauOP
I thought it can be an issue as you said Yes just above so
becuase before I create this Issue before that I saw 2 more guys have the same issue, but not sure it's in App Router or Pages Route
@fuma it shouldn't, because I'm doing the same in my project and it works perfectly
Egyptian MauOP
This significantly speeds up return navigations to the page, however some browser APIs (e.g. unload listeners) can cause the bfcache to fail and the page will be loaded normally. -- source https://developer.chrome.com/docs/lighthouse/performance/bf-cache/?utm_source=lighthouse&utm_medium=devtools
Egyptian MauOP
any help?
I'm willing to help if you can provide a minimal reproducible example
I can confirm it works in my Next.js applications
@fuma I'm willing to help if you can provide a minimal reproducible example
Egyptian MauOP
or I can give you the temp access to my GitHub Repo where the code is
Can you reproduce the problem in a new project?
@fuma Can you reproduce the problem in a new project?
Egyptian MauOP
I don't know from which code part is making an issue, as I've many codes, that's why I asked
@fuma Can you reproduce the problem in a new project?
Egyptian MauOP
I am asking to share GitHub Repo access because I don't know from which part it's throwing the error
Egyptian MauOP
'use client'
// Nextjs
import Image from 'next/image'
// import { usePathname } from 'next/navigation'
// MUI
import { AppBar, Box, Container, IconButton, Toolbar } from '@mui/material'
import MenuIcon from '@mui/icons-material/Menu'
// Data
import { NavbarDataArray } from '@/data'
// UI
import { NavbarLinkTypography, NextLink } from '@/ui'
// Image from Public Dir
import CodeXplorer57Logo from '../../../../public/PNGLogo/CodeXplorer_Logo_57x57.png'
interface DesktopNavbarProps {
handleDrawerToggle: () => void
}
const DesktopNavbar = ({ handleDrawerToggle }: DesktopNavbarProps) => {
// const pathname = usePathname()
return (
<Box>
<AppBar component="nav" sx={{ background: 'transparent', boxShadow: 'none', position: 'absolute' }}>
<Container maxWidth="xl">
<Toolbar sx={{ height: '70px', minHeight: '70px' }}>
<IconButton aria-label="open drawer" edge="start" onClick={handleDrawerToggle} sx={{ mr: 2, display: { sm: 'none' }, color: '#000' }}>
<MenuIcon />
</IconButton>
<Box sx={{ flexGrow: { md: 1, xs: 0 }, ml: 'auto' }}>
<NextLink href="/" aria-label="CodeXplorer Home" title="CodeXplorer Home">
<Image src={CodeXplorer57Logo} alt="CodeXplorer" title="CodeXplorer" loading="lazy" />
</NextLink>
</Box>
<Box sx={{ flexGrow: 3, display: { xs: 'none', md: 'flex' }, justifyContent: { xs: 'space-between', md: 'flex-end' } }}>
{NavbarDataArray.map((data, idx) => {
return (
<NavbarLinkTypography key={idx}>
<NextLink href={data?.url || '/'}>
{data?.label}
</NextLink>
</NavbarLinkTypography>
)
})}
</Box>
</Toolbar>
</Container>
</AppBar>
</Box>
)
}
export default DesktopNavbarthis is my navbar for desktop screen
'use client'
// React
import React, { Fragment } from 'react'
// MUI
import { Box, Divider, Drawer, List, ListItem, ListItemButton, ListItemText } from '@mui/material'
// UI
import { NavbarLinkTypography, NextLink } from '@/ui'
// Data
import { NavbarDataArray } from '@/data'
// Sidebar Drawer Width
const drawerWidth = 250
interface MobileNavbarProps {
mobileOpen: boolean
handleDrawerToggle: () => void
}
const MobileNavbar = ({ mobileOpen, handleDrawerToggle }: MobileNavbarProps) => {
const drawer = (
<Box onClick={handleDrawerToggle} sx={{ display: 'flex', flexDirection: 'column', justifyContent: 'space-between', height: '100%' }}>
<List>
{NavbarDataArray.map((data, idx) => {
return (
<Fragment key={idx}>
<ListItem disablePadding>
<ListItemButton sx={{ ml: 2 }}>
<NextLink href={data?.url || '/'} aria-label={data?.label} title={data?.label}>
<ListItemText primary={<NavbarLinkTypography>{data?.label}</NavbarLinkTypography>} />
</NextLink>
</ListItemButton>
</ListItem>
<Divider />
</Fragment>
)
})}
</List>
</Box>
)
return (
<Box component="nav">
<Drawer
variant="temporary"
open={mobileOpen}
onClose={handleDrawerToggle}
ModalProps={{
keepMounted: true,
}}
sx={{
display: { xs: 'block', sm: 'none' },
zIndex: 99999,
'& .MuiDrawer-paper': {
boxSizing: 'border-box',
width: drawerWidth,
},
}}
>
{drawer}
</Drawer>
</Box>
)
}
export default MobileNavbarthis the navbar for mobile screen
'use client'
import { useState } from 'react'
// MUI
import { Box, Toolbar } from '@mui/material'
// Navbars
import DesktopNavbar from './Responsive/Desktop'
import MobileNavbar from './Responsive/Mobile'
const Navbar = () => {
const [mobileOpen, setMobileOpen] = useState(false)
const handleDrawerToggle = () => setMobileOpen(!mobileOpen)
return (
<Box>
<DesktopNavbar handleDrawerToggle={handleDrawerToggle} />
<MobileNavbar mobileOpen={mobileOpen} handleDrawerToggle={handleDrawerToggle} />
<Box component="main">
<Toolbar />
</Box>
</Box>
)
}
export default Navbarand this is the main Navbar file which contains both Desktop + Mobile Navbar Code
and here,
NextLink I am changing the style for it which is as below:'use client'
// Nextjs
import Link from 'next/link'
// MUI
import { styled } from '@mui/material'
// Fonts
import { JostFont } from './font'
export const NextLink = styled(Link)({
textDecoration: 'none',
color: '#000',
fontFamily: JostFont.style.fontFamily,
'&:hover': {
textDecoration: 'none',
color: '#000',
},
})Egyptian MauOP
@fuma
Do not ping moderators for code help
Egyptian MauOP
okay!
@joulev Do *not* ping moderators for code help
Egyptian MauOP
I wasn't getting the solution, that's why I tagged the moderators
Egyptian MauOP
any help on this is appreciated, as I noticed that same issue was on GitHub, but due to no activity it wsa closed
Egyptian MauOP
any help on this issue?
I’m sorry but it’s unlikely I can reproduce the error with just a few code snippets (and I’m not familiar with MUI), you may seek some help from their community instead.
@Egyptian Mau I don't know from which code part is making an issue, as I've many codes, that's why I asked
Try removing the parts until the error is gone, that helps for debugging it
Egyptian MauOP
which parts?
Every parts of your UI
Egyptian MauOP
ohhhh