Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure current() method respects 'absolute' option #353

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Breaking changes are marked with ⚠️.
- ⚠️ Make the `nameKeyedRoutes()`, `resolveBindings()`, `applyFilters()`, and `group()` methods on the `Ziggy` class, and the `generate()` method on the `CommandRouteGenerator` class, private ([#341](https://github.com/tighten/ziggy/pull/341))
- ⚠️ Export from `index.js` instead of `route.js` ([#344](https://github.com/tighten/ziggy/pull/344))
- ⚠️ Encode boolean query parameters as integers ([#345](https://github.com/tighten/ziggy/pull/345))
- ⚠️ Ensure `.current()` respects the value of the `absolute` option ([#353](https://github.com/tighten/ziggy/pull/353))

**Deprecated**

Expand Down
2 changes: 1 addition & 1 deletion src/js/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Route {
this.name = name;
this.definition = definition;
this.bindings = definition.bindings ?? {};
this.config = { absolute: true, ...config };
this.config = config;
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/js/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export default class Router extends String {
super();

this._config = config ?? Ziggy ?? globalThis?.Ziggy;
this._config = { ...this._config, absolute };

if (name) {
if (!this._config.routes[name]) {
throw new Error(`Ziggy error: route '${name}' is not in the route list.`);
}

this._route = new Route(name, this._config.routes[name], { ...this._config, absolute });
this._route = new Route(name, this._config.routes[name], this._config);
this._params = this._parse(params);
}
}
Expand Down Expand Up @@ -68,7 +69,9 @@ export default class Router extends String {
* @return {(Boolean|String)}
*/
current(name, params) {
const url = window.location.host + window.location.pathname;
const url = this._config.absolute
? window.location.host + window.location.pathname
: window.location.pathname.replace(this._config.url.replace(/^\w*:\/\/[^/]+/, ''), '').replace(/^\/+/, '/');

// Find the first route that matches the current URL
const [current, route] = Object.entries(this._config.routes).find(
Expand Down
28 changes: 28 additions & 0 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,34 @@ describe('current()', () => {
same(route().current(), 'events.venues.index');
});

test('can ignore domain when getting current route name and absolute is false', () => {
global.window.location.href = 'https://tighten.ziggy.dev/events/1/venues?foo=2';
global.window.location.host = 'tighten.ziggy.dev';
global.window.location.pathname = '/events/1/venues?foo=2';

same(route(undefined, undefined, false).current(), 'events.venues.index');

global.window.location.href = 'https://example.com/events/1/venues?foo=2';
global.window.location.host = 'example.com';
global.window.location.pathname = '/events/1/venues?foo=2';

same(route(undefined, undefined, false).current(), 'events.venues.index');
});

test('can ignore domain when getting current route name, absolute is false, and app is in a subfolder', () => {
global.Ziggy.url = 'https://tighten.ziggy.dev/subfolder';
global.window.location.href = 'https://tighten.ziggy.dev/subfolder/events/1/venues?foo=2';
global.window.location.pathname = '/subfolder/events/1/venues?foo=2';

same(route(undefined, undefined, false).current(), 'events.venues.index');

global.Ziggy.url = 'https://example.com/nested/subfolder';
global.window.location.href = 'https://example.com/nested/subfolder/events/1/venues?foo=2';
global.window.location.pathname = '/nested/subfolder/events/1/venues?foo=2';

same(route(undefined, undefined, false).current(), 'events.venues.index');
});

test('can get the current route name with a custom Ziggy object', () => {
global.Ziggy = undefined;
global.window.location.pathname = '/events/';
Expand Down