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

Follow redirects in the loader #1099

Merged
merged 3 commits into from
Mar 21, 2024
Merged

Follow redirects in the loader #1099

merged 3 commits into from
Mar 21, 2024

Conversation

jurgenwerk
Copy link
Contributor

@jurgenwerk jurgenwerk commented Mar 18, 2024

This PR adds an internal loader functionality where it will follow redirects of responses returned by the loader's URL handlers.

This addition has a couple of really good advantages:

  1. Removing a lot of test setup and mocks
  2. Conforming to fetch api spec (response.url needs to contain the final url in the redirection chain, and response.redirected must be true in case of redirection)
  3. Removing a small obstacle in our ongoing efforts to refactor the loaders to be isolated (the less url handlers in tests the better)

Copy link

github-actions bot commented Mar 18, 2024

Test Results

531 tests  ±0   527 ✔️ ±0   7m 14s ⏱️ -1s
    1 suites ±0       4 💤 ±0 
    1 files   ±0       0 ±0 

Results for commit 6f9d6d1. ± Comparison against base commit 4eed5de.

♻️ This comment has been updated with latest results.

@jurgenwerk jurgenwerk requested a review from a team March 18, 2024 12:29
@jurgenwerk jurgenwerk marked this pull request as ready for review March 18, 2024 12:29
test('is able to follow redirects', async function (assert) {
loader.prependURLHandlers([
async (request) => {
if (request.url.includes('node-b.abc')) {
Copy link
Contributor

@tintinthong tintinthong Mar 18, 2024

Choose a reason for hiding this comment

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

whats the 'c' in abc. Is it clearer if its node-b.ab??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.abc is just a url domain I made up.

@@ -495,17 +495,53 @@ export class Loader {
}
}

// For following redirects of responses returned by loader's urlHandlers
private async simulateFetch(
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is good in that it removes the assumptions of the mocked response. Tho the mocked response was contained in tests.

Does this code path actually get passed other than tests?

Copy link
Contributor Author

@jurgenwerk jurgenwerk Mar 18, 2024

Choose a reason for hiding this comment

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

I think there is currently no url handler in the non-test code that would result in a redirect.

Tests use maybeHandle and that one does result in a redirect in some cases (e.g. /sth/person -> sth/person.gts when the mime type indicates code source)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is currently no url handler in the non-test code that would result in a redirect.

Not in tests, I think most redirects occur when clicking the pills. The url fetched is /sth/person. I think maybeHandle does handle that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, I would think that in that case the native fetch is used which follows redirections automatically (by default)

Comment on lines +514 to +520
// We are using Object.defineProperty because `url` and `redirected`
// response properties are read-only. We are overriding these properties to
// conform to the Fetch API specification where the `url` property is set to
// the final URL and the `redirected` property is set to true if the request
// was redirected. Normally, when using a native fetch, these properties are
// set automatically by the client, but in this case, we are simulating the
// fetch and need to set these properties manually.
Copy link
Contributor

Choose a reason for hiding this comment

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

super useful

Copy link
Contributor

@tintinthong tintinthong left a comment

Choose a reason for hiding this comment

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

But yea, my conservatism previously to do the mocks was to not pollute the code that runs thru the loader. But I acknowledge that its kinda weird. I believe this is a better change

@jurgenwerk
Copy link
Contributor Author

jurgenwerk commented Mar 18, 2024

@tintinthong I see, yeah I remember you explained the reasons for mocking in #667. During our recent pairings on the loader refactor we decided it's a better idea to put that functionality right in the loader, mainly to conform to the fetch spec and that the acceptance tests don't have to know about redirections

@jurgenwerk jurgenwerk requested review from a team, lukemelia, habdelra and ef4 March 19, 2024 07:05
Copy link
Contributor

@habdelra habdelra left a comment

Choose a reason for hiding this comment

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

great reduction in test support code 👍

@jurgenwerk jurgenwerk merged commit 3f37e30 into main Mar 21, 2024
18 checks passed
@delete-merged-branch delete-merged-branch bot deleted the follow-redirects-in-loader branch March 21, 2024 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants