Next.js 14 Conversion & Server-side vs. Client-side Conditional

We’ve lost our primary frontend developer and I’m having to step in midway through an upgrade from Next.js 12 to Next.js 14. I’m not particularly proficient in either version and I’ve long struggling identifying code meant for server-side rendering vs. client-side rendering. This is exasperated by the introduction of the app router we’ve begun moving towards.

Below is a trimmed down example of a page that was ported into the app router.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { JSX } from 'react';
import { Metadata } from 'next';
import { env } from '@/lib/utils';
export const metadata: Metadata = {
title: 'Test',
};
export default function Page(): JSX.Element {
return (
<>
<div className='pt-8'>
<div className='mt-1 grid grid-cols-1 gap-3'>
Something, Something, Something, Dark Side...
</div>
</div>
{(env('E2E_ENV') === 'true') && (
<>
<div className='mt-6 relative'>
You're our only hope!
</div>
</>
)}
</>
);
}
</code>
<code>import { JSX } from 'react'; import { Metadata } from 'next'; import { env } from '@/lib/utils'; export const metadata: Metadata = { title: 'Test', }; export default function Page(): JSX.Element { return ( <> <div className='pt-8'> <div className='mt-1 grid grid-cols-1 gap-3'> Something, Something, Something, Dark Side... </div> </div> {(env('E2E_ENV') === 'true') && ( <> <div className='mt-6 relative'> You're our only hope! </div> </> )} </> ); } </code>
import { JSX } from 'react';
import { Metadata } from 'next';
import { env } from '@/lib/utils';

export const metadata: Metadata = {
    title: 'Test',
};

export default function Page(): JSX.Element {
    return (
        <>
            <div className='pt-8'>
                <div className='mt-1 grid grid-cols-1 gap-3'>
                    Something, Something, Something, Dark Side...
                </div>
            </div>

            {(env('E2E_ENV') === 'true') && (
                <>
                    <div className='mt-6 relative'>
                        You're our only hope!
                    </div>
                </>
            )}
        </>
    );
}

From @/lib/utils:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Simplified from https://github.com/andrewmclagan/react-env/blob/master/packages/node/src/index.js
export function env(key = '') {
console.log(key+' env = '+process.env[key])
if (isBrowser() && window.__env) {
console.log(key+' __env = '+(window.__env[key] === "''" ? '' : window.__env[key]))
return window.__env[key] === "''" ? '' : window.__env[key]
}
return process.env[key] === "''" ? '' : process.env[key]
}
</code>
<code>// Simplified from https://github.com/andrewmclagan/react-env/blob/master/packages/node/src/index.js export function env(key = '') { console.log(key+' env = '+process.env[key]) if (isBrowser() && window.__env) { console.log(key+' __env = '+(window.__env[key] === "''" ? '' : window.__env[key])) return window.__env[key] === "''" ? '' : window.__env[key] } return process.env[key] === "''" ? '' : process.env[key] } </code>
// Simplified from https://github.com/andrewmclagan/react-env/blob/master/packages/node/src/index.js
export function env(key = '') {
    console.log(key+' env = '+process.env[key])
    if (isBrowser() && window.__env) {
        console.log(key+' __env = '+(window.__env[key] === "''" ? '' : window.__env[key]))
        return window.__env[key] === "''" ? '' : window.__env[key]
    }
    return process.env[key] === "''" ? '' : process.env[key]
}

This previously worked fine with the E2E_ENV variable being picked up to display the enclosed HTML when appropriate, and env() always just worked given its compatibility with either server-side or client-side use.

This also works perfectly fine in our development environment, where env() can be seen running server-side.

The problem comes when it’s built for non-development use. env() is then seen operating client-side instead of server-side and while it still returns the expected value, the enclosed HTML is never displayed. That is to say, the condition looks like it should be testing true as the call and result is present in the browser console, but the additional HTML content isn’t rendered.

I’m sure I’m just running afoul of client-/server-side principles and the fix is probably stupid simple, but I could use a direct example of how to make this right. That’ll hopefully help me understand the underlying principles that I’ve had a hell of a time with so far.

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