kriasoft / universal-router

A simple middleware-style router for isomorphic JavaScript web apps
https://www.kriasoft.com/universal-router/
MIT License
1.7k stars 104 forks source link

Full url - with origin, not just the path #142

Closed AhmadMayo closed 6 years ago

AhmadMayo commented 6 years ago

Is their a way to get the full url? I define the route like this '/home' but Sometimes I want to generate the route line this 'www.myawesomeapp.com/home'

frenzzy commented 6 years ago

How about this?

import UniversalRouter from 'universal-router';
import generateUrls from 'universal-router/generateUrls';

const router = new UniversalRouter({ name: 'home', path: '/home' });

const path = generateUrls(router);
const url = (...args) => `https://www.example.com${path(...args)}`;

path('home'); // => /home
url('home');  // => https://www.example.com/home

Playground: http://jsfiddle.net/frenzzy/jwcbme5x/

AhmadMayo commented 6 years ago

already thought of that, but having development server in local machine, staging server and a production server makes it's hard to hard code the origin

frenzzy commented 6 years ago

Then you can use environment variables in your server-side code instead of hard coded values or use current host aka req.get('host').

On client side you can use the origin which was returned from backend side or window.location.origin

AhmadMayo commented 6 years ago

Thinking about it, as universal router is "universal" it should not contain logic related to the browser. thanks for your time, I'm gona close the issue, and try to figure out the solution

AhmadMayo commented 6 years ago

Great idea. Also I can solve the problem from the history package I'm using, that sounds more appropriate. where's the "close issue" button? :smile: . Please close the issue.