Giter VIP home page Giter VIP logo

next-api-url's Introduction

Next.js Absolute API URL Helper

roniemeque

This package provides a quick solution around the common Next.js error that happens while trying to fetch data in the server side without providing a full url:

Server Error: Only absolute URLs are supported

Why does this happen though? Since these data fetching functions run only on the server, they have no way of knowing the origin like code on the client side does, so Next.js reminds you that an absolute URL has to be provided.

Warning: this helper only works for getServerSideProps and getInitialProps (both client and server) -- For getStaticProps you should either hard code the URL or use enviroment variables.

Usage

Simply provide the context to the function:

import apiUrl from "next-api-url";

fetch(`${apiUrl(ctx)}/posts`); // http://localhost:3000/api
fetch(`${apiUrl(ctx)}/posts`); // https://blog.com/api
fetch(`${apiUrl({ req: ctx.req })}/posts`); // https://blog.com/api
fetch(`${apiUrl({ req: ctx.req })}/posts`, false); // https://blog.com

Examples

getServerSideProps

import apiUrl from "next-api-url";

export const getServerSideProps: GetServerSideProps = async (context) => {
  const posts = await (await fetch(`${apiUrl(context)}/api/posts`)).json();

  return {
    props: { posts },
  };
};

getInitialProps (works on server and client ๐Ÿ˜Š)

import apiUrl from "next-api-url";

Page.getInitialProps = async (context) => {
  const posts = await (await fetch(`${apiUrl(context)}/api/posts`)).json();

  return {
    posts,
  };
};

getServerSideProps using wrapper

import { withApiUrl } from "next-api-url";

export const getServerSideProps = withApiUrl(async (context, url) => {
  const posts = await (await fetch(`${url}/api/posts`)).json();

  return {
    props: {
      posts,
    },
  };
});

getInitialProps using wrapper

import { withApiUrl } from "next-api-url";

Page.getInitialProps = withApiUrl(async (context, url) => {
  const posts = await (await fetch(`${url}/api/posts`)).json();

  return {
    posts,
  };
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.