Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Respect GET params#987

Closed
vytautas-pranskunas- wants to merge 4 commits intoangular:masterfrom
vytautas-pranskunas-:master
Closed

Respect GET params#987
vytautas-pranskunas- wants to merge 4 commits intoangular:masterfrom
vytautas-pranskunas-:master

Conversation

@vytautas-pranskunas-
Copy link
Copy Markdown

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[X] Bugfix
[X] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

Cache is not respecting GET params

What is the new behavior?

Cache is respecting GET params

Does this PR introduce a breaking change?

[ ] Yes
[X] No

Other information

No matter that my API is pure JSON and i am not using GET I fixed bug that GET requests where not cached by params. Just one smal remark - for the same reason that I am not using GET i was not able to test. I tested with POST so GET should work similar. Please test befor merge.

@googlebot
Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

Copy link
Copy Markdown

@Toxicable Toxicable left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do the following:

  • Undo the formatting changes. It's hard to identify what you've changed when you've reformatted the whole file.
  • Squash commits.
  • Sign the CLA
  • Make the commit something similar to: feat(@nguniversal/common): allow cache to work with url params

@vytautas-pranskunas-
Copy link
Copy Markdown
Author

Sry do not have time for it. What I done -

  1. joined params to object, stringify it and hashed.
constructCacheKey method ...
  1. Also i added check to not pass request through if transferState has value for it
if (!this.isCacheActive && !this.transferState.hasKey(storeKey)) {
            // Cache is no longer active. Pass the request through.
            return next.handle(req);
        }
  1. Added invalidation after retrieving value from transferState
if (this.transferState.hasKey(storeKey)) {
            // Request found in cache. Respond using it.
            const response = this.transferState.get(storeKey, {} as TransferHttpResponse);
            this.invalidateCacheEntry(storeKey);
...

let key = `${req.method[0]}_${req.url}`;

key = key
.replace(/[^\w\s]/gi, '_');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what this is doing?
A comment should suffice here

Copy link
Copy Markdown
Author

@vytautas-pranskunas- vytautas-pranskunas- May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this just beutify key. Because url can be with slashes so it replaces all not letter characters with '_'.

return key;
}

private hashParams(body: any) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this hashing method?
Is it some well known method?
Why not use something off the shelf?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is taken from stackowerlof. I thought why to take another dependency for such simple hashing functionality.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment to credit the author and source

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add that as a comment in your src code

@Toxicable
Copy link
Copy Markdown

@vytautas-pranskunas- If you cant resolve the above comments then we cannot accept this PR.

If that's ok with you then I'll implement the changes when I next get a chance

return { key: req.params.get(key) };
});

key += this.hashSeparator + this.hashParams(hashedParams);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it this way means if someone has the params in a different order it'll casue a new request.
EG
foo=2&bar=3 produces key of 25
bar=3&foo=2 produces key of 52

When since their content is the same they should produce the same key

Copy link
Copy Markdown
Author

@vytautas-pranskunas- vytautas-pranskunas- May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. But usually queries are fixed ant their order not changing. But ofcourse if you want - you can sort arasm before applying hashing.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, param order should not matter. A reliable, case-sensitive (!) sort by key seems reasonable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you want to say that in one query it can be user=..&User=... and in another visa versa? Sounds like a smell...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The keys should be case senstive.
Meaning that the keys user and User are not equal.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added comment about sorting you asked

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't mean to make a comment about this, it needs to be applied aswell.
Merging this without the sorting will be broken.

@angular angular deleted a comment from googlebot May 16, 2018
@Toxicable
Copy link
Copy Markdown

Closed in favour of #1005

@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants