Skip to content

Follow redirects in the loader#1099

Merged
jurgenwerk merged 3 commits into
mainfrom
follow-redirects-in-loader
Mar 21, 2024
Merged

Follow redirects in the loader#1099
jurgenwerk merged 3 commits into
mainfrom
follow-redirects-in-loader

Conversation

@jurgenwerk

@jurgenwerk jurgenwerk commented Mar 18, 2024

Copy link
Copy Markdown
Contributor

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)

@github-actions

github-actions Bot commented Mar 18, 2024

Copy link
Copy Markdown
Contributor

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')) {

@tintinthong tintinthong Mar 18, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

}

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

Copy link
Copy Markdown
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?

@jurgenwerk jurgenwerk Mar 18, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

super useful

@tintinthong tintinthong left a comment

Copy link
Copy Markdown
Contributor

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

jurgenwerk commented Mar 18, 2024

Copy link
Copy Markdown
Contributor Author

@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, ef4, habdelra and lukemelia March 19, 2024 07:05

@habdelra habdelra left a comment

Copy link
Copy Markdown
Contributor

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
@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