-
-
Notifications
You must be signed in to change notification settings - Fork 633
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
[WIP] Hydrate components immediately after downloading chunks #1656
base: abanoubghadban/pro362-add-support-for-RSC
Are you sure you want to change the base?
Changes from all commits
9973bb1
00943df
98b08ca
82c9d80
2b79735
0c233e4
0d7bae6
a545a3c
999312d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,13 +44,72 @@ def create_pack(file_path) | |
puts(Rainbow("Generated Packs: #{output_path}").yellow) | ||
end | ||
|
||
def first_js_statement_in_code(content) | ||
return "" if content.nil? || content.empty? | ||
|
||
start_index = 0 | ||
content_length = content.length | ||
|
||
while start_index < content_length | ||
# Skip whitespace | ||
start_index += 1 while start_index < content_length && content[start_index].match?(/\s/) | ||
|
||
break if start_index >= content_length | ||
|
||
current_chars = content[start_index, 2] | ||
|
||
case current_chars | ||
when "//" | ||
# Single-line comment | ||
newline_index = content.index("\n", start_index) | ||
return "" if newline_index.nil? | ||
|
||
start_index = newline_index + 1 | ||
when "/*" | ||
# Multi-line comment | ||
comment_end = content.index("*/", start_index) | ||
return "" if comment_end.nil? | ||
|
||
start_index = comment_end + 2 | ||
else | ||
# Found actual content | ||
next_line_index = content.index("\n", start_index) | ||
return next_line_index ? content[start_index...next_line_index].strip : content[start_index..].strip | ||
end | ||
end | ||
|
||
"" | ||
end | ||
|
||
def is_client_entrypoint?(file_path) | ||
content = File.read(file_path) | ||
# has "use client" directive. It can be "use client" or 'use client' | ||
first_js_statement_in_code(content).match?(/^["']use client["'](?:;|\s|$)/) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is all this necessary? Client & server entrypoints should be detectable by their filenames. |
||
|
||
def pack_file_contents(file_path) | ||
registered_component_name = component_name(file_path) | ||
load_server_components = ReactOnRails::Utils.react_on_rails_pro? && ReactOnRailsPro.configuration.enable_rsc_support | ||
|
||
if load_server_components && !is_client_entrypoint?(file_path) | ||
import_statement = "" | ||
rsc_rendering_url_path = ReactOnRailsPro.configuration.rsc_rendering_url_path | ||
register_call = <<~REGISTER_CALL.strip | ||
registerServerComponent({ | ||
rscRenderingUrlPath: "#{rsc_rendering_url_path}", | ||
}, "#{registered_component_name}") | ||
REGISTER_CALL | ||
else | ||
relative_component_path = relative_component_path_from_generated_pack(file_path) | ||
import_statement = "import #{registered_component_name} from '#{relative_component_path}';" | ||
register_call = "register({#{registered_component_name}})" | ||
end | ||
|
||
<<~FILE_CONTENT | ||
import ReactOnRails from 'react-on-rails'; | ||
import #{registered_component_name} from '#{relative_component_path_from_generated_pack(file_path)}'; | ||
#{import_statement} | ||
|
||
ReactOnRails.register({#{registered_component_name}}); | ||
ReactOnRails.#{register_call}; | ||
FILE_CONTENT | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,8 +50,8 @@ def stale_generated_files(files) | |
def all_compiled_assets | ||
@all_compiled_assets ||= begin | ||
webpack_generated_files = @webpack_generated_files.map do |bundle_name| | ||
if bundle_name == ReactOnRails.configuration.server_bundle_js_file | ||
ReactOnRails::Utils.server_bundle_js_file_path | ||
if bundle_name == ReactOnRails.configuration.react_client_manifest_file | ||
ReactOnRails::Utils.react_client_manifest_file_path | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't make sense. Neither the Also, I don't see why you would want to replace the |
||
else | ||
ReactOnRails::Utils.bundle_js_file_path(bundle_name) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ItemRegistrationCallback } from "./types"; | ||
|
||
export default class CallbackRegistry<T> { | ||
private registeredItems = new Map<string, T>(); | ||
private callbacks = new Map<string, Array<ItemRegistrationCallback<T>>>(); | ||
|
||
set(name: string, item: T): void { | ||
this.registeredItems.set(name, item); | ||
|
||
const callbacks = this.callbacks.get(name) || []; | ||
callbacks.forEach(callback => { | ||
setTimeout(() => callback(item), 0); | ||
}); | ||
this.callbacks.delete(name); | ||
} | ||
|
||
get(name: string): T | undefined { | ||
return this.registeredItems.get(name); | ||
} | ||
|
||
has(name: string): boolean { | ||
return this.registeredItems.has(name); | ||
} | ||
|
||
clear(): void { | ||
this.registeredItems.clear(); | ||
} | ||
|
||
getAll(): Map<string, T> { | ||
return this.registeredItems; | ||
} | ||
|
||
onItemRegistered(name: string, callback: ItemRegistrationCallback<T>): void { | ||
const existingItem = this.registeredItems.get(name); | ||
if (existingItem) { | ||
setTimeout(() => callback(existingItem), 0); | ||
return; | ||
} | ||
|
||
const callbacks = this.callbacks.get(name) || []; | ||
callbacks.push(callback); | ||
this.callbacks.set(name, callbacks); | ||
} | ||
|
||
getOrWaitForItem(name: string): Promise<T> { | ||
return new Promise((resolve) => { | ||
this.onItemRegistered(name, resolve); | ||
}); | ||
} | ||
} |
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.
How do we make sure that registered stores that aren't listed in the
store_dependencies
don't slip through the cracks?Should we throw a warning if a store is registered that isn't listed in
store_dependencies
?