How to communicate page properties to the layout in nextjs?

I’m using the app mode in a new nextjs project, and I have the requirement to update the favicon so that it’s different for different pages. I am doing this by having a global object like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>interface ILayoutParams
{
favicon: string
}
const LayoutParams: ILayoutParams = {} as any
</code>
<code>interface ILayoutParams { favicon: string } const LayoutParams: ILayoutParams = {} as any </code>
interface ILayoutParams
{
    favicon: string
}

const LayoutParams: ILayoutParams = {} as any

And having each child page set this property:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const ThisPage = () =>
{
LayoutParams.favicon = "This"
return <>content...</>
}
const ThatPage = () =>
{
LayoutParams.favicon = "That"
return <>content...</>
}
</code>
<code>const ThisPage = () => { LayoutParams.favicon = "This" return <>content...</> } const ThatPage = () => { LayoutParams.favicon = "That" return <>content...</> } </code>
const ThisPage = () =>
{
    LayoutParams.favicon = "This"

    return <>content...</>
}

const ThatPage = () =>
{
    LayoutParams.favicon = "That"

    return <>content...</>
}

and, finally, rendering the icon like so in the layout component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> metadata.icons =
{
icon: `/images/favicon/${LayoutParams.favicon}.ico`,
}
</code>
<code> metadata.icons = { icon: `/images/favicon/${LayoutParams.favicon}.ico`, } </code>
    metadata.icons = 
    {
        icon: `/images/favicon/${LayoutParams.favicon}.ico`,
    }

This does work: the mysite.com/this page loads and shows the correct icon This.ico

However, when I navigate to another page mysite.com/that, it does NOT update the icon. If I then hard refresh the page, it again shows the correct icon.

I assume this is because the navigation is not actually loading the page, but swapping it in client-side, and this bypasses my LayoutParams scheme. But I cannot find any way to make it work the way I want.

What is the right way to pass child page properties to the layout page in such a way that it works on the server and the client?

You’re probably right in saying that this approach won’t work because of how navigation is managed: layouts in fact don’t re-render on navigation.

I think you could just use this approach with a custom function that returns a different icon based on the url params.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// app/shop/[slug]/icon.tsx
export default function Icon({ params }: { params: { slug: string } }) {
// if (slug === 'this') return <ThisIcon/>
// if (slug === 'that') return <ThatIcon/>
}
</code>
<code>// app/shop/[slug]/icon.tsx export default function Icon({ params }: { params: { slug: string } }) { // if (slug === 'this') return <ThisIcon/> // if (slug === 'that') return <ThatIcon/> } </code>
// app/shop/[slug]/icon.tsx
export default function Icon({ params }: { params: { slug: string } }) {
  // if (slug === 'this') return <ThisIcon/>
  // if (slug === 'that') return <ThatIcon/>
}

here the docs about making it based on the page you’re on, using the url params.

Alternatively, you could use a dedicated icon.tsx file for every page where a specific favicon is needed.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// app/this-page/icon.tsx
import { ImageResponse } from 'next/og'
// Image metadata
export const size = {
width: 32,
height: 32,
}
export const contentType = 'image/png'
// Image generation
export default function Icon() {
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 24,
background: 'black',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
}}
>
A
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported icons size metadata
// config to also set the ImageResponse's width and height.
...size,
}
)
}
</code>
<code>// app/this-page/icon.tsx import { ImageResponse } from 'next/og' // Image metadata export const size = { width: 32, height: 32, } export const contentType = 'image/png' // Image generation export default function Icon() { return new ImageResponse( ( // ImageResponse JSX element <div style={{ fontSize: 24, background: 'black', width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'white', }} > A </div> ), // ImageResponse options { // For convenience, we can re-use the exported icons size metadata // config to also set the ImageResponse's width and height. ...size, } ) } </code>
// app/this-page/icon.tsx
import { ImageResponse } from 'next/og'
 
// Image metadata
export const size = {
  width: 32,
  height: 32,
}
export const contentType = 'image/png'
 
// Image generation
export default function Icon() {
  return new ImageResponse(
    (
      // ImageResponse JSX element
      <div
        style={{
          fontSize: 24,
          background: 'black',
          width: '100%',
          height: '100%',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          color: 'white',
        }}
      >
        A
      </div>
    ),
    // ImageResponse options
    {
      // For convenience, we can re-use the exported icons size metadata
      // config to also set the ImageResponse's width and height.
      ...size,
    }
  )
}

Mind that “The default export function should return a Blob | ArrayBuffer | TypedArray | DataView | ReadableStream | Response” https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons#returns

EDIT: since this got me curious, there is another very “silly” solution. Just putting the relevant image as an “icon.png (or .ico or whatever)” directly in the interested page folder.
screenshot

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật