-
-
Notifications
You must be signed in to change notification settings - Fork 260
perf(homepage): prerender the highlighted code examples [Codex] #1364
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
Open
jderochervlk
wants to merge
4
commits into
perf/lazy-docsearch
from
perf/prerender-homepage-highlighting
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cd1396e
perf(homepage): prepare code examples in the route loader
jderochervlk b55ca73
test(highlighting): port prepared homepage checks to Cypress
jderochervlk ce1e7ee
refactor(highlighting): centralize loader bindings
jderochervlk 149251f
test(homepage): port highlighting Cypress spec to ReScript
jderochervlk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| open Vitest | ||
|
|
||
| test( | ||
| "homepage example loader produces only serializable highlighted code and its URL", | ||
| async () => { | ||
| let data = LandingPagePlaygroundLoader.build() | ||
| let serialized = data->JSON.stringifyAny->Option.getOrThrow | ||
| let decoded = JSON.parseOrThrow(serialized) | ||
| let expected = JSON.Object( | ||
| Dict.fromArray([ | ||
| ("rescriptHtml", JSON.String(data.rescriptHtml)), | ||
| ("javascriptHtml", JSON.String(data.javascriptHtml)), | ||
| ("playgroundHref", JSON.String(data.playgroundHref)), | ||
| ]), | ||
| ) | ||
|
|
||
| expect(decoded)->toEqual(expected) | ||
| expect(data.rescriptHtml->String.includes("<span"))->toBe(true) | ||
| expect(data.javascriptHtml->String.includes("<span"))->toBe(true) | ||
| let {pathname, searchParams} = WebAPI.URL.make( | ||
| ~url=data.playgroundHref, | ||
| ~base="https://rescript-lang.org", | ||
| ) | ||
| let code = searchParams->WebAPI.URLSearchParams.get("code") | ||
|
|
||
| expect(pathname)->toBe("/try") | ||
| expect(LzString.lzString.decompressFromEncodedURIComponent(code))->toBe( | ||
| LandingPageFixture.expectedExample, | ||
| ) | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| /** Use hosted Inter faces within the homepage without changing other routes. */ | ||
| @react.component | ||
| let make: unit => React.element | ||
| let make: (~playgroundData: LandingPagePlayground.playgroundData) => React.element |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| open Cypress | ||
| open HomepageHelpers | ||
|
|
||
| it("initial homepage scripts do not include example preparation or compression", () => { | ||
| homepageDocument(document => { | ||
| let scripts = document->initialScriptUrls | ||
| expect(scripts->Array.length)->greaterThan(0) | ||
| scripts->Array.forEach( | ||
| asset => | ||
| request(asset) | ||
| ->then( | ||
| response => { | ||
| expect(response.status, ~message=asset)->equal(200) | ||
| expect(response.body, ~message=asset)->notInclude("compressToEncodedURIComponent") | ||
| expect(response.body, ~message=asset)->notInclude("function Playground$Button(props)") | ||
| }, | ||
| ) | ||
| ->ignore, | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| it("prepared examples survive hydration and navigation back from documentation", () => { | ||
| homepageDocument(document => { | ||
| let examples = ["res", "js"]->Array.map( | ||
| language => { | ||
| let selector = `code.lang-${language}` | ||
| let html = | ||
| document | ||
| ->querySelector(selector) | ||
| ->Null.toOption | ||
| ->Option.map(element => element->innerHTML) | ||
| ->Option.getOrThrow | ||
| (selector, html) | ||
| }, | ||
| ) | ||
| let playgroundHref = | ||
| document | ||
| ->querySelector(`a[href^="/try?code="]`) | ||
| ->Null.toOption | ||
| ->Option.flatMap(element => element->getAttribute("href")->Null.toOption) | ||
| ->Option.getOrThrow | ||
| expect(playgroundHref)->match_(/^\/try\?code=.+/) | ||
| examples->Array.forEach(((_, html)) => expect(html)->include_(`<span class="hljs-`)) | ||
| visit("/") | ||
| examples->Array.forEach( | ||
| ((selector, html)) => get(selector)->shouldProperty("innerHTML", html)->ignore, | ||
| ) | ||
| containsInRegex("a", /^Docs$/)->click->ignore | ||
| cyLocation("pathname")->shouldEqual("/docs/manual/introduction")->ignore | ||
| get(`a[aria-label="homepage"]`)->click->ignore | ||
| cyLocation("pathname")->shouldEqual("/")->ignore | ||
| examples->Array.forEach( | ||
| ((selector, html)) => | ||
| get(selector)->should("be.visible")->shouldProperty("innerHTML", html)->ignore, | ||
| ) | ||
| containsIn("a", "Edit this example in Playground") | ||
| ->shouldAttribute("href", playgroundHref) | ||
| ->ignore | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| type t | ||
| type language | ||
| type highlightOptions = {language: string} | ||
| type highlightResult = {value: string} | ||
|
|
||
| @module("highlight.js/lib/core") @scope("default") | ||
| external make: unit => t = "newInstance" | ||
|
|
||
| @module("highlight.js/lib/languages/javascript") | ||
| external javascript: language = "default" | ||
|
|
||
| @module("highlightjs-rescript") | ||
| external rescript: language = "default" | ||
|
|
||
| @send | ||
| external registerLanguage: (t, string, language) => unit = "registerLanguage" | ||
|
|
||
| @send | ||
| external highlight: (t, string, highlightOptions) => highlightResult = "highlight" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,9 @@ | ||
| type playgroundData = { | ||
| rescriptHtml: string, | ||
| javascriptHtml: string, | ||
| playgroundHref: string, | ||
| } | ||
|
|
||
| /** Render only trusted highlighted markup produced by the homepage loader. */ | ||
| @react.component | ||
| let make: unit => React.element | ||
| let make: (~playgroundData: playgroundData) => React.element |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.