page.js icon indicating copy to clipboard operation
page.js copied to clipboard

Incorrect pathname and querystring

Open onury opened this issue 9 years ago • 4 comments

When you click on a bookmark of a page with a query-string, context has incorrect pathname and querystring.

Example:

Page URL is http://domain.com/products/?id=10 which includes the following link:

<a href="#bookmark">link</a>

When clicked, URL is:

http://domain.com/products/?id=10#bookmark

At this point,

window.location returns:

{ 
    hash: "#bookmark", 
    search: "?id=10", 
    pathname: "/products/",
    href: "http://domain.com/products/?id=10#bookmark",
    ...
}

but

page.js context returns:

{ 
    hash: "bookmark", 
    querystring: "",  // should be "id=10"
    pathname: "/products/#bookmark",  // ?
    path: "/",
    ...
}

Page.js version 1.7.1 Chrome version 50.0.2661.102

onury avatar May 29 '16 17:05 onury

What does your use of page() look like here? What route are you listening to?

matthewp avatar Jan 13 '18 17:01 matthewp

@matthewp It's simple:

page('(/)?products', (context, next) => {
    console.log(context);
});
page('(/)?', (context, next) => {
    console.log(context);
});

onury avatar Jan 13 '18 23:01 onury

Thanks! I'll try to take a look.

matthewp avatar Jan 14 '18 00:01 matthewp

Leaving this info here if someone musters the energy to send a PR

Pathname is set to URL without string content after ?. https://github.com/visionmedia/page.js/blob/4f9991658f9b9e3de9b6059bade93693af24d6bd/page.mjs#L1084

But when hasbang is false and URL has a hash, pathname and path are set to the initial full path. https://github.com/visionmedia/page.js/blob/4f9991658f9b9e3de9b6059bade93693af24d6bd/page.mjs#L1089-L1092

Soo navigating to a URL that has an hash and a question mark will cause wrong pathnames.

http://example.com/products/?id=1#bookmark

Pickachu avatar Nov 14 '20 14:11 Pickachu