rewrite api menggunakan next 14

Settings next config mjs

next.config.mjs
/** @type {import('next').NextConfig} */
const nextConfig = {
   async rewrites() {
    return [
      {
        source: '/js-holder/:path*',
        destination: 'https://jsonplaceholder.typicode.com/:path*'
      }
    ]
  },
};

export default nextConfig;
NEXT_PUBLIC_URL=http://localhost:3000
app.tsx
 try {
    const response = await fetch(process.env.NEXT_PUBLIC_URL + '/js-holder/todos/1');

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.json(); // Parse JSON response
    return { data }
  } catch (error) {
    console.error('Error fetching data:', error);
 }

Contact us

Perumahan Griya Pratama Asri 2 No.H-12, Somoragen, Joho, Kec. Prambanan, Kabupaten Klaten, Jawa Tengah 57454
Find Me
iconiconiconiconhttps://www.buymeacoffee.com/outyourimpossible
2024-PRESENT © Muh Syahendra A