File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11'use client' ;
22
3+ import { API_URL } from '@/lib/utils' ;
34import { useRouter } from 'next/navigation' ;
45import { useEffect } from 'react' ;
56
@@ -10,7 +11,7 @@ export const Preview = () => {
1011 const handleMessage = async ( message : MessageEvent < any > ) => {
1112 const { origin, data } = message ;
1213
13- if ( origin !== process . env . NEXT_PUBLIC_API_URL ) {
14+ if ( origin !== API_URL ) {
1415 return ;
1516 }
1617
Original file line number Diff line number Diff line change 1+ import { API_URL } from '@/lib/utils' ;
12import { unstable_noStore as noStore } from 'next/cache' ;
23import Image from 'next/image' ;
34import { ComponentProps } from 'react' ;
@@ -9,17 +10,11 @@ interface StrapiImageProps
910}
1011
1112export function getStrapiMedia ( url : string | null ) {
12- const strapiURL = process . env . NEXT_PUBLIC_API_URL ;
1313 if ( url == null ) return null ;
1414 if ( url . startsWith ( 'data:' ) ) return url ;
1515 if ( url . startsWith ( 'http' ) || url . startsWith ( '//' ) ) return url ;
16- if ( url . startsWith ( '/' ) ) {
17- if ( ! strapiURL && document ?. location . host . endsWith ( '.strapidemo.com' ) ) {
18- return `https://${ document . location . host . replace ( 'client-' , 'api-' ) } ${ url } ` ;
19- }
20- return strapiURL + url ;
21- }
22- return `${ strapiURL } ${ url } ` ;
16+
17+ return API_URL + url ;
2318}
2419
2520export function StrapiImage ( {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { strapi } from '@strapi/client';
22import type { API , Config } from '@strapi/client' ;
33import { cacheLife , cacheTag , revalidateTag } from 'next/cache' ;
44import { draftMode } from 'next/headers' ;
5+ import { API_URL } from '../utils' ;
56
67export class StrapiError extends Error {
78 constructor (
@@ -19,7 +20,7 @@ const createClient = (
1920 isDraftMode : boolean = false
2021) => {
2122 return strapi ( {
22- baseURL : `${ process . env . NEXT_PUBLIC_API_URL ?? '' } /api` ,
23+ baseURL : `${ API_URL } /api` ,
2324 headers : {
2425 'strapi-encode-source-maps' : isDraftMode ? 'true' : 'false' ,
2526 ...config ?. headers ,
Original file line number Diff line number Diff line change 11import { unstable_noStore as noStore } from 'next/cache' ;
2+ import { API_URL } from '../utils' ;
23
34export function strapiImage ( url : string ) : string {
45 noStore ( ) ;
5- if ( url . startsWith ( '/' ) ) {
6- if (
7- ! process . env . NEXT_PUBLIC_API_URL &&
8- document ?. location . host . endsWith ( '.strapidemo.com' )
9- ) {
10- return `https://${ document . location . host . replace ( 'client-' , 'api-' ) } ${ url } ` ;
11- }
126
13- return process . env . NEXT_PUBLIC_API_URL + url ;
7+ if ( url . startsWith ( '/' ) ) {
8+ return API_URL + url ;
149 }
10+
1511 return url ;
1612}
Original file line number Diff line number Diff line change @@ -19,3 +19,6 @@ export const formatNumber = (
1919 maximumFractionDigits : 2 ,
2020 } ) . format ( number ) ;
2121} ;
22+
23+ export const API_URL = process . env . NEXT_PUBLIC_API_URL ||
24+ ( globalThis . document ?. location . host . endsWith ( '.strapidemo.com' ) ? `https://${ document . location . host . replace ( 'client-' , 'api-' ) } ` : '' ) ;
Original file line number Diff line number Diff line change 11/** @type {import('next').NextConfig } */
22const nextConfig = {
3+ output : process . env . NEXT_OUTPUT || undefined ,
34 // Enable Next.js 16 cache components
45 cacheComponents : true ,
56 turbopack : {
@@ -28,6 +29,13 @@ const nextConfig = {
2829 } ,
2930 pageExtensions : [ 'ts' , 'tsx' ] ,
3031 async redirects ( ) {
32+ if ( process . env . NEXT_PUBLIC_API_URL === undefined ) {
33+ console . warn (
34+ '[next.config] NEXT_PUBLIC_API_URL is not defined. Skipping redirect generation.'
35+ ) ;
36+ return [ ] ;
37+ }
38+
3139 let redirections = [ ] ;
3240 try {
3341 const res = await fetch (
You can’t perform that action at this time.
0 commit comments