How to fix Next.JS Error: only absolute urls are supported?
Last Updated :
19 Jun, 2024
In Next.JS, One common issue we encounter is that "only absolute URLs are supported" when making HTTP requests or navigating to routes within the next.js application. only absolute URLs are supported" this error occurs when a function is expecting a absolute URL (one that includes the protocol and domain) for communication rather, it got a absolute URL then this error is thrown.
There are different approaches that can be used to fix this error:
Fetching Data from API
When using fetch or axios to make HTTP requests, you need to provide an absolute URL.
// pages/index.js
import { useEffect, useState } from 'react';
export default function Home() {
const [data, setData] = useState(null);
useEffect(() => {
async function fetchData() {
// Absolute URL
const res = await fetch('https://api.example.com/data');
const json = await res.json();
setData(json);
}
fetchData();
}, []);
return (
<div>
<h1>Data</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
Using axios
- If you prefer using axios, the setup is similar:
// pages/index.js
import { useEffect, useState } from 'react';
import axios from 'axios';
export default function Home() {
const [data, setData] = useState(null);
useEffect(() => {
async function fetchData() {
const res = await axios.get('https://api.example.com/data'); // Absolute URL
setData(res.data);
}
fetchData();
}, []);
return (
<div>
<h1>Data</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
Navigating Between Routes
Using next.js routing programmatically with next/link or next/router ensure you are using proper absolute or relative URLs depending on the context.
Using next/link
- When linking to internal routes, next/link works with relative URLs:
// pages/index.js
import Link from 'next/link';
export default function Home() {
return (
<div>
<h1>Home Page</h1>
<Link href="/https/www.geeksforgeeks.org/about">
<a>Go to About Page</a>
</Link>
</div>
);
}
Using next/router
- When programmatically navigating, next/router can handle both absolute and relative URLs.
- Use relative URLs for internal navigation:
// pages/index.js
import { useRouter } from 'next/router';
export default function Home() {
const router = useRouter();
const navigateToAbout = () => {
router.push('/about'); // Relative URL
};
return (
<div>
<h1>Home Page</h1>
<button onClick={navigateToAbout}>Go to About Page</button>
</div>
);
}
Handling API Routes in Next.js
If you’re making requests to internal API routes, use relative URLs for client-side requests and absolute URLs for server-side requests.
Client-Side Requests
For client-side requests, use relative URLs:
// pages/index.js
import { useEffect, useState } from 'react';
export default function Home() {
const [data, setData] = useState(null);
useEffect(() => {
async function fetchData() {
const res = await fetch('/api/data'); // Relative URL
const json = await res.json();
setData(json);
}
fetchData();
}, []);
return (
<div>
<h1>Data</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}
Server-Side Requests
For server-side requests, use absolute URLs, especially if you're making requests to external APIs:
// pages/api/data.js
export default async function handler(req, res) {
// Absolute URL
const response = await fetch('https://api.example.com/data');
const data = await response.json();
res.status(200).json(data);
}
Conclusion
"only absolute URLs are supported" This error in Next.JS can be solved by ensuring the usage of the absolute URL(one that includes the protocol and domain) for API usage avoiding the relative URLs. This relative URLs are used within the nexts.js application but when dealing with outer server we must ensure using the absolute URLs as shown as the above steps. This ensures the error doesn't occur when dealing with axios or HTTP requests.
Similar Reads
How to Fix CORS Errors in Next.js and Vercel?
Cross-Origin Resource Sharing (CORS) is a security feature that restricts web apps from making requests to a different domain than the one that served the web page to it. This is needed for security, although sometimes it can block legitimate requests, especially when working with APIs. In this arti
4 min read
How to Set up Docker and NGINX for a Next.js?
In the world of microservices, it is necessary to containerize our applications for deployments. It makes life easier for developers, allowing them to build and test applications in a controlled environment. Containers provide isolation, meaning that each instance runs independently of others, preve
3 min read
How to resolve a "Cannot find module" error using Node.js?
This article outlines the steps you can take to troubleshoot and fix "Cannot find module" errors in your Node.js projects. These errors typically arise when Node.js cannot locate a module you're trying to use in your code. Table of Content Error "Cannot find module" Approach to Solve the ErrorInstal
3 min read
How to use Next.js API Routes?
Next.js API Routes are a feature of Next.js that allows you to create server-side logic and APIs within your Next.js application. These API routes are implemented using files in the `pages/api` directory of your Next.js project. When you deploy your Next.js application, these API routes are automati
8 min read
How to Fix Error EJS 'partial is not defined'?
This article provides a detailed explanation of the ejs 'partial is not defined' error, detailing various approaches to resolve it, from using native EJS features to creating custom functions. The error 'partial is not defined' occurs in EJS when you use a partial function that hasn't been defined.
3 min read
How to fix "Error: NextRouter was not mounted" ?
The error "Error: NextRouter was not mounted" typically occurs when trying to use the NextRouter context outside of a Next.js <Router> provider. This error indicates that a component is attempting to access the router instance before it has been properly mounted by Next.js. In this guide, we'l
3 min read
How to Use the App Directory in Next.js?
The App Directory in Next.js provides a powerful way to structure and manage your application's pages and components. It simplifies the routing and organization of your project, enabling you to build complex web applications with ease.Understanding the App DirectoryThe App Directory is a new structu
3 min read
How to Fix the "partial is not defined" Error in EJS ?
Encountering an error message like "partial is not defined" can be a stumbling block when you're working with EJS (Embedded JavaScript) templates. This error typically pops up when you're trying to use the `partial` function in EJS, but it's not being recognized. Don't worry, though it's a common hi
3 min read
How to fix "Next.js: document is not defined"?
The "document is not defined" error in next js happens because the document is a client-side browser object, which isn't available during server-side rendering. In this article, we'll explain why this error occurs and how to fix it.Table of ContentWhen does this error occur?Steps for Fixing Document
5 min read
How to Disable the ETag Generation in Next.js ?
ETags (Entity Tags) are a mechanism used by HTTP to validate the cache of resources and improve performance by avoiding unnecessary data transfers. In Next.js, ETags are generated by default for API routes to optimize caching. However, in some cases, you may need to disable ETag generation, such as
3 min read