-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[Fizz][Float] Refactor Resources #27400
Conversation
6574df7
to
7eafe3d
Compare
db27abb
to
ccbb26c
Compare
precedences: Map<string, PrecedenceQueue>, | ||
bootstrapScripts: Set<Resource>, | ||
scripts: Set<Resource>, | ||
bulkPreloads: Set<Resource>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this just be preloads.unknown
? Am I understanding that connection, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, image is currently modeled as a known resource type that also sometimes flushes in this queue. I would say that the queue names should probably be about priority and not tied directly to the resource types. I do want to rename though, just haven't though of the right naming scheme and since this will potentially need to change with upcoming work on Suspense everywhere I think I'll just leave it as is for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preloads.other
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be grouped with the others though, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been thinking about the preloads.images
as well as the other preloads....
maps as a working state to reason about "upgrading" preloads to other kinds of resources (or in the case of images just upgrading its priority). However There really isn't a conflict in using them as flushing queues. Once consequence though is that it bakes in a sort of precedence because we can only empty these working queues one at a time whereas script, style, image, and arbitrary preloads today can flush in discovery order in the bulkPreloads queue.
I don't think it makes a huge difference either way and I kind of like separating the "keep track of our preload states so we can act on them later" and "here is the order I want to flush things"
fontPreloads: Set<Resource>, | ||
highImagePreloads: Set<Resource>, | ||
// usedImagePreloads: Set<Resource>, | ||
precedences: Map<string, PrecedenceQueue>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I don't really like the name precedence here because 1) we can easily have other fizz concepts named that and this is specifically about styles 2) the ultimate terminal content of this isn't the flushing precedence it's the stylesheets. It happens to be grouped by precedence but it's about flushing the styles.
Proposal:
styles: Map<string, StyleQueue>
It's a queue for styles, it contains a queue of styles which are grouped by precedence. The queue happens to have a chunk attribute annotating the precedence/name of this queue but its primary purpose is to be a queue of styles. In theory it could also have a nullable precedence and be reused into specific like built-in styles queues that lives outside of the precedence system. E.g. if we had a built-in reset or something that always goes first like external runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I like this
// to convey. It should never be checked by identity and we should not | ||
// assume Preload values in ResumableState equal this value because they | ||
// will have come from some parsed input. | ||
const PRELOAD_SIGIL: Preloaded = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't necessarily call this a sigil because unlike other sigils, the referential identity of this doesn't matter. Since when it goes to/from JSON it'll be multiple copies. It's a reusable value but it doesn't imply that it has to be this specific instance to indicate that's a preload.
ccbb26c
to
7223017
Compare
…Style tag Resources. The Style tag resource was already handled with special rules because it does not Flush is the same way as other resources do. This change reimagines the precedences as a combination of a style tag rules queue and a list of stylesheets. This change eliminates a number of conditionals by embedding these variants in the data represenation itself. This change also moves the binary encoding to work time rather than flush time. Since style resources were the only type that actually used the `type` property I've removed this making resource object size slightly smaller
Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state. Stylesheets and Style tags are different and have been modeled as a unit as a Precedence. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late. The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed.
… not have a loadable type and we identify these as UnknownResources. For example fonts and images are preloadable but not actually loadable so we bucket them in this unknown category We use an array to identify a preload state. I've cleaned up the implementaiton of preloads for bootstrap scripts and modules I've also added support for nonce for preloadModule and preinitModule.
… is. A precedence is now always the string value and the precedence queue is the thing that holds info about style tag rules and stylesheets that need to be flushed for that precedence
…be put in a different flushing queue or may need to be prevented from flushing. This only applies to a few resource types however, stylesheet, images, scripts, and moduleScripts. Another problem is that our resources are keyed using a compound key of href and the resource type but our preloadsMap only used href. This change also updates the preloads tracking object to have a similar compound key structure so there is not chance of collisions based on same href for different resources types (however rare this is in practice)
… resources are created there is at most one preload per resource. This affects stylesheets, scripts, and moduleScripts only
… render a styletag with the href using precedence. This is probably done as a mistake (accidentally using the same href) but if you are going to inline on your own then you should also take care not to preload
…havior when an img tag is rendered and we promote the priority
7223017
to
c003f8a
Compare
Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state. Stylesheets and Style tags are different and have been modeled as a unit as a StyleQueue. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late. The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed. DiffTrain build for [49eba01](49eba01)
React upstream changes: - facebook/react#27439 - facebook/react#26763 - facebook/react#27434 - facebook/react#27433 - facebook/react#27424 - facebook/react#27428 - facebook/react#27427 - facebook/react#27315 - facebook/react#27314 - facebook/react#27400 - facebook/react#27421 - facebook/react#27419 - facebook/react#27418
Today when we hydrate an SSR'd RSC response on the client we encounter import chunks which initiate code loading for client components. However we only start fetching these chunks after hydration has begun which is necessarily after the initial chunks for the entrypoint have loaded. React has upstream changes that need to land which will preinitialize the rendered chunks for all client components used during the SSR pass. This will cause a `<script async="" src... />` tag to be emitted in the head for each chunk we need to load during hydration which allows the browser to start fetching these resources even before the entrypoint has started to execute. Additionally the implementation for webpack and turbopack is different enough that there will be a new `react-server-dom-turbopack` package in the React repo which should be used when using Turbopack with Next. This PR also removes a number of patches to React src that proxy loading (`__next_chunk_load__`) and bundler requires (`__next_require__`) through the `globalThis` object. Now the react packages can be fully responsible for implementing chunk loading and all Next needs to do is supply the necessary information such as chunk prefix and crossOrigin attributes necessary for this loading. This information is produced as part of the client-manifest by either a Webpack plugin or Turbopack. Additionally any modifications to the chunk filename that were previously done at runtime need to be made in the manifest itself now. This means we need to encode the deployment id for skew protection and encode the filename to make it match our static path matching (and resolutions on s3) when using `[` and `]` segment characters. There are a few followup items to consider in later PRs 1. we currently bundle a node and edge version of react-server-dom-webpack/client. The node version has an implementation for busboy whereas the edge version does not. Next is currently configured to use busboy when handling a fetch action sent as multipart with a node runtime. Ideally we'd only bundle the one platform we are buliding for but some additional refactoring to support better forking is possibly required here This PR also updates react from 09285d5a7 to d900fadbf. ### React upstream changes - facebook/react#27439 - facebook/react#26763 - facebook/react#27434 - facebook/react#27433 - facebook/react#27424 - facebook/react#27428 - facebook/react#27427 - facebook/react#27315 - facebook/react#27314 - facebook/react#27400 - facebook/react#27421 - facebook/react#27419 - facebook/react#27418
Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state. Stylesheets and Style tags are different and have been modeled as a unit as a StyleQueue. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late. The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed.
Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state. Stylesheets and Style tags are different and have been modeled as a unit as a StyleQueue. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late. The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed. DiffTrain build for commit 49eba01.
Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state.
Stylesheets and Style tags are different and have been modeled as a unit as a StyleQueue. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late.
The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed.