Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jakeburden/next-absolute-url
Get the absolute URL of your Next.js app (optionally set a localhost dev URL)
https://github.com/jakeburden/next-absolute-url
Last synced: 14 days ago
JSON representation
Get the absolute URL of your Next.js app (optionally set a localhost dev URL)
- Host: GitHub
- URL: https://github.com/jakeburden/next-absolute-url
- Owner: jakeburden
- Created: 2019-02-11T00:15:58.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-04T00:17:27.000Z (over 1 year ago)
- Last Synced: 2024-05-17T19:02:37.668Z (6 months ago)
- Language: TypeScript
- Size: 760 KB
- Stars: 301
- Watchers: 3
- Forks: 30
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# next-absolute-url
> Get the protocol and host for the absolute URL of your Next.js app (and optionally set a dev url)
This module enables you to easily get the protocol and host of your Next.js app, both on the server and the client. Optionally, you can set a localhost variable, which is useful for local development if you have local lambda functions running on a different port.
## Usage
> I'm honored that an excellent blog post has been published about this package. For detailed usage, please see: https://web.archive.org/web/20211110110818/https://codeconqueror.com/blog/get-the-current-url-in-next-js/
```js
import absoluteUrl from 'next-absolute-url'
const { protocol, host } = absoluteUrl(req, 'localhost:8004')
const apiURL = `${protocol}//${host}/api/job.js`
```or if you just want the full URL origin:
```js
import absoluteUrl from 'next-absolute-url'
const { origin } = absoluteUrl(req)
const apiURL = `${origin}/api/job.js`
```If you deployed your Next.js app with `now` the `apiURL` will be something like `https://your-app.now.sh/api/job.js`.
However, if you are running the app locally the `apiURL` will be `http://localhost:8004/api/job.js` instead.
## Install
With [npm](https://npmjs.org/) installed, run
```sh
npm install next-absolute-url
```MIT