Skip to content

Commit df474c9

Browse files
authored
Merge pull request #114 from strapi/demo-use-experimental
Fix previews on demo infrastructure
2 parents 3eca1f6 + 9a53356 commit df474c9

6 files changed

Lines changed: 22 additions & 18 deletions

File tree

next/components/preview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client';
22

3+
import { API_URL } from '@/lib/utils';
34
import { useRouter } from 'next/navigation';
45
import { 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

next/components/ui/strapi-image.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { API_URL } from '@/lib/utils';
12
import { unstable_noStore as noStore } from 'next/cache';
23
import Image from 'next/image';
34
import { ComponentProps } from 'react';
@@ -9,17 +10,11 @@ interface StrapiImageProps
910
}
1011

1112
export 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

2520
export function StrapiImage({

next/lib/strapi/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { strapi } from '@strapi/client';
22
import type { API, Config } from '@strapi/client';
33
import { cacheLife, cacheTag, revalidateTag } from 'next/cache';
44
import { draftMode } from 'next/headers';
5+
import { API_URL } from '../utils';
56

67
export 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,

next/lib/strapi/strapiImage.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import { unstable_noStore as noStore } from 'next/cache';
2+
import { API_URL } from '../utils';
23

34
export 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
}

next/lib/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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-')}` : '');

next/next.config.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** @type {import('next').NextConfig} */
22
const 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(

0 commit comments

Comments
 (0)