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

Make ClientRequest chop off fragment identifiers from urls #972

Merged
merged 1 commit into from
Dec 1, 2017
Merged
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
Made ClientRequest chop off fragment identifiers from urls
Joshua LEO-Irabor committed Dec 1, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 2388c6a68900a1256d92541e49ecb0141c0f535d
14 changes: 11 additions & 3 deletions packages/react-server/core/ClientRequest.js
Original file line number Diff line number Diff line change
@@ -10,11 +10,19 @@ class ClientRequest {
bundleData,
reuseDom,
}={}) {
this._url = url;
this._opts = {
bundleData,
reuseDom,
}

// Chop off the fragment identifier from the url i.e everything from the # to the end of the url
// if it exists to make this consistent with ExpressServerRequest.
var match = url.match(/([^#]*)/);
if (match === null || !match[1]) {
this._url = url
} else {
this._url = match[1];
}
}

setRoute(route) {
@@ -38,8 +46,8 @@ class ClientRequest {
}

getQuery() {
//Grab fragment between first "?" and first "#" or end of string
var match = this._url.match(/\?([^#]*)/);
// Grab fragment after first "?"
var match = this._url.match(/\?(.*)/);

if (match === null || !match[1]) {
return {};
22 changes: 22 additions & 0 deletions packages/react-server/core/__tests__/ClientRequestSpec.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,26 @@ describe("ClientRequest", () => {
clientRequest = new ClientRequest("/");
});

it("removes fragment identifiers from the url", (done) => {
clientRequest = new ClientRequest("/");
expect(clientRequest.getUrl()).toEqual("/");
clientRequest = new ClientRequest("/react-server/foo#bar");
expect(clientRequest.getUrl()).toEqual("/react-server/foo");
clientRequest = new ClientRequest("/react-server/foo/#bar#bazz");
expect(clientRequest.getUrl()).toEqual("/react-server/foo/");
clientRequest = new ClientRequest("/react-server/foo/?#bar");
expect(clientRequest.getUrl()).toEqual("/react-server/foo/?");
clientRequest = new ClientRequest("/react-server/foo/?foo=bar&baz=123");
expect(clientRequest.getUrl()).toEqual("/react-server/foo/?foo=bar&baz=123");
clientRequest = new ClientRequest("/react-server/foo/?foo=bar&baz=123#");
expect(clientRequest.getUrl()).toEqual("/react-server/foo/?foo=bar&baz=123");
clientRequest = new ClientRequest("/react-server/foo/?foo=bar&baz=123&zed=abc?#some-fragment?#");
expect(clientRequest.getUrl()).toEqual("/react-server/foo/?foo=bar&baz=123&zed=abc?");
clientRequest = new ClientRequest("/react-server/foo#?bar=3&foo=7");
expect(clientRequest.getUrl()).toEqual("/react-server/foo");
done();
});

it("parses query params correctly", (done) => {
clientRequest = new ClientRequest("/");
expect(clientRequest.getQuery()).toEqual({});
@@ -24,6 +44,8 @@ describe("ClientRequest", () => {
expect(clientRequest.getQuery()).toEqual({foo: "bar", baz: "123"});
clientRequest = new ClientRequest("/react-server/foo/?foo=bar&baz=123&zed=abc?#some-fragment?#");
expect(clientRequest.getQuery()).toEqual({foo: "bar", baz: "123", zed: "abc?"});
clientRequest = new ClientRequest("/react-server/foo#?bar=3&foo=7");
expect(clientRequest.getQuery()).toEqual({});
done();
});