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

Integration with preload #1342

Merged
merged 12 commits into from
Feb 11, 2022
Merged

Integration with preload #1342

merged 12 commits into from
Feb 11, 2022

Conversation

noamr
Copy link
Contributor

@noamr noamr commented Oct 26, 2021

Before any particular fetch steps are performed, see if there is a
matching request already in the preload store and consume it.

This is called from the main fetch to avoid race conditions.

Depends on whatwg/html#7260, and together they fix #590.

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff


Preview | Diff

@noamr noamr changed the title Attempt to reuse preloaded responses. Integration with preload Oct 26, 2021
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
@noamr
Copy link
Contributor Author

noamr commented Nov 4, 2021

@annevk I've revised this, whatwg/html#7260 would also need some changes to match, but I wanted to see that this is on the right track first.

The idea is that preloads are matched before the fetch becomes parallel and overrides all the HTTP specific check as it's assumed they were done for the preloaded request.

An alternative to doing things this way is to have the preload list inside fetch, and have the HTML spec add to it / clear it. This might make things easier in terms of future potential things like supporting Link headers in workers, but I'm not sure if that should be a concern right now (also doing that is not difficult inside the HTML spec).

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

Generally I think this can work, but it will depend a bit on the other details.

I'm also not sure where the cache itself should be defined. I can see arguments either way.

fetch.bs Outdated Show resolved Hide resolved
@annevk
Copy link
Member

annevk commented Nov 4, 2021

Actually, I'm not sure. Doing a cache lookup synchronously seems bad, but perhaps that is what implementations do? That probably warrants a bit more investigation. One way to do it later in "main fetch" would be to check the value of recursive I suppose, although that might still run into problems with HTTP authentication requests.

@noamr
Copy link
Contributor Author

noamr commented Nov 4, 2021

Actually, I'm not sure. Doing a cache lookup synchronously seems bad, but perhaps that is what implementations do? That probably warrants a bit more investigation. One way to do it later in "main fetch" would be to check the value of recursive I suppose, although that might still run into problems with HTTP authentication requests.

Cache lookup should be synchronous IMO as it relies on something controlled by the main thread, making it parallel would create either locks or unnecessary races.

Maybe keeping it where I put it is correct?

@noamr
Copy link
Contributor Author

noamr commented Nov 7, 2021

See whatwg/html#7260 (comment)

noamr added a commit to web-platform-tests/wpt that referenced this pull request Nov 8, 2021
Check that preloaded resources are reused/ignored based on the
parameters available to a preload link (href, crossorigin, as),
and do not rely on other parameters (e.g. whether a script is a module).

See whatwg/html#7260
and whatwg/fetch#1342
noamr added a commit to web-platform-tests/wpt that referenced this pull request Nov 8, 2021
Check that preloaded resources are reused/ignored based on the
parameters available to a preload link (href, crossorigin, as),
and do not rely on other parameters (e.g. whether a script is a module).

See whatwg/html#7260
and whatwg/fetch#1342
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
noamr added a commit to web-platform-tests/wpt that referenced this pull request Dec 1, 2021
Check that preloaded resources are reused/ignored based on the
parameters available to a preload link (href, crossorigin, as),
and do not rely on other parameters (e.g. whether a script is a module).

See whatwg/html#7260
and whatwg/fetch#1342
noamr added a commit to web-platform-tests/wpt that referenced this pull request Dec 1, 2021
Check that preloaded resources are reused/ignored based on the
parameters available to a preload link (href, crossorigin, as),
and do not rely on other parameters (e.g. whether a script is a module).

See whatwg/html#7260
and whatwg/fetch#1342
noamr added a commit to web-platform-tests/wpt that referenced this pull request Dec 1, 2021
* Add tests to verify preload caching behavior

Check that preloaded resources are reused/ignored based on the
parameters available to a preload link (href, crossorigin, as),
and do not rely on other parameters (e.g. whether a script is a module).

See whatwg/html#7260
and whatwg/fetch#1342

* Add missing font

* whitespace

* Cleanup

* PR nits

* PR nits

* More nits
Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

Where does this end up invoking finalize response?

fetch.bs Outdated
<a for=request>header list</a>, if <var>header</var>'s <a for=header>name</a> is
not `<code>If-Modified-Since</code>`, `<code>If-None-Match</code>`, `<code>If-Unmodified-Since</code>`,
`<code>If-Match</code>`, `<code>User-Agent</code>`, or `<code>Cache-Control</code>`, set
<var>hasCustomHeaders</var> to true.
Copy link
Member

Choose a reason for hiding this comment

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

This would always end up with hasCustomHeaders being true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How come?
Anyway, maybe we should avoid hasCustomHeaders at all, and Let fetch() set allow preloads to false if there are user-defined headers.

Copy link
Member

Choose a reason for hiding this comment

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

I had misread it, but it also seems the requirement is different now. Any author-defined headers triggers it now. But doesn't that also apply to XMLHttpRequest?

fetch.bs Outdated
<li><p><var>request</var>'s <a for=request>allow preloads</a> is true

<li><p><var>request</var>'s <a for=request>origin</a> is <var>request</var>'s
<a for=request>client</a>'s <a for="environment settings object">origin</a>
Copy link
Member

Choose a reason for hiding this comment

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

When would this not be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's theoretically possible to call fetch with a different origin, the only place where I found this to happen is Process a navigate fetch which is not relevant for preloads. Maybe this can be an assert?

fetch.bs Outdated
<ol>
<li><p>Let <var>onPreloadedResponseAvailable</var> be to an algorithm that runs the following
step <a>in parallel</a> given <a for=/>response</a> <var>response</var>:
Run <a>fetch finale</a> given <var>response</var> and <var>fetchParams</var>.
Copy link
Member

Choose a reason for hiding this comment

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

Wrapping needs to be adjusted here. Run should be lowercase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

@noamr
Copy link
Contributor Author

noamr commented Dec 3, 2021

Where does this end up invoking finalize response?

Here. When the response is available, fetch finale is called and should run finalize response (once #1311 is in that part would be a lot cleaner)

fetch.bs Outdated
<a>fetch finale</a> given <var>response</var> and <var>fetchParams</var>.

<li><p>Let <var>foundPreloadedResource</var> be the result of invoking
<span>consume a preloaded resource</span> for <var>req</var>'s <a for=request>window</a>,
Copy link
Member

Choose a reason for hiding this comment

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

This should use <a>.

fetch.bs Outdated
<a for=request>header list</a>, if <var>header</var>'s <a for=header>name</a> is
not `<code>If-Modified-Since</code>`, `<code>If-None-Match</code>`, `<code>If-Unmodified-Since</code>`,
`<code>If-Match</code>`, `<code>User-Agent</code>`, or `<code>Cache-Control</code>`, set
<var>hasCustomHeaders</var> to true.
Copy link
Member

Choose a reason for hiding this comment

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

I had misread it, but it also seems the requirement is different now. Any author-defined headers triggers it now. But doesn't that also apply to XMLHttpRequest?

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

When would we expect to use "allow preloads"? If it's just XHR and fetch() I think we have enough state on request to make that determination already.

@noamr
Copy link
Contributor Author

noamr commented Dec 7, 2021

When would we expect to use "allow preloads"? If it's just XHR and fetch() I think we have enough state on request to make that determination already.

Custom headers currently should disallow preloads, but also things like "synchronous XHR", which I don't know if we have the knowledge for at this point.

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

To clarify, this depends on the other PR that merges everything into "fetch finale", right?

fetch.bs Outdated
<p>If all of the following conditions are true:</p>

<ul class=brief>
<li><p><var>request</var>'s <a for=request>destination</a> is not "<code>document</code>"
Copy link
Member

Choose a reason for hiding this comment

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

Should this use https://fetch.spec.whatwg.org/#navigation-request instead? The only overlap I can see with mode is for embed/object. Otherwise this would be redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually this line is wrong. It's ok to preload a document.

fetch.bs Outdated Show resolved Hide resolved
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
@noamr
Copy link
Contributor Author

noamr commented Dec 13, 2021

To clarify, this depends on the other PR that merges everything into "fetch finale", right?

I don't think it needs to be dependent. Fetch finale clarifies things but doesn't change the behavior of this.
Because the response is already fully loaded into a byte stream by the time it's returned from the "consume" algorithm (marking the response "done"), the current version of fetch finale would work just the same.

@noamr
Copy link
Contributor Author

noamr commented Dec 16, 2021

@annevk: another review?

moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Dec 20, 2021
…vior, a=testonly

Automatic update from web-platform-tests
Add tests to verify preload caching behavior (#31539)

* Add tests to verify preload caching behavior

Check that preloaded resources are reused/ignored based on the
parameters available to a preload link (href, crossorigin, as),
and do not rely on other parameters (e.g. whether a script is a module).

See whatwg/html#7260
and whatwg/fetch#1342

* Add missing font

* whitespace

* Cleanup

* PR nits

* PR nits

* More nits
--

wpt-commits: ef427f550f2a2a803fb2fa0886951e559fc83d9c
wpt-pr: 31539
@noamr
Copy link
Contributor Author

noamr commented Jan 17, 2022

@annevk: another review?

fetch.bs Show resolved Hide resolved
Copy link
Collaborator

@yoavweiss yoavweiss left a comment

Choose a reason for hiding this comment

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

Non-authoritative LGTM

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

Do the changes I made look okay to you?

@noamr
Copy link
Contributor Author

noamr commented Feb 11, 2022

Do the changes I made look okay to you?

Sure!

@annevk
Copy link
Member

annevk commented Feb 11, 2022

@noamr where is the work tracked on the "memory cache"? To some extent it was tracked as part of the preload issue before.

@noamr
Copy link
Contributor Author

noamr commented Feb 11, 2022

@noamr where is the work tracked on the "memory cache"? To some extent it was tracked as part of the preload issue before.

What was tracked before is the relationship between preload and the memory cache. The way it is spec'ed now, the relationship with requests and preloads is simple - if the CORS-mode and URL match then the preloaded resource should be consumed, otherwise ignored (re-fetched).

On WebKit and Gecko this is not the case (because of the "memory cache" the CORS mode is ignored) and I submitted a WPT and opened relevant bugs.

I do believe the memory cache should be better defined, but it's not currently tracked.

@annevk annevk merged commit 8ebf2fd into whatwg:main Feb 11, 2022
@annevk
Copy link
Member

annevk commented Feb 11, 2022

Filed #1400.

@annevk
Copy link
Member

annevk commented Feb 11, 2022

@noamr could you link the bugs from OP?

@noamr
Copy link
Contributor Author

noamr commented Feb 11, 2022

@noamr could you link the bugs from OP?

Not sure what OP is.
https://bugzilla.mozilla.org/show_bug.cgi?id=1743764
https://bugs.webkit.org/show_bug.cgi?id=233854

@noamr noamr deleted the preload branch February 11, 2022 13:46
@annevk
Copy link
Member

annevk commented Feb 11, 2022

It stands for original post(er) in a thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Define the "Preload Cache"
3 participants