close
The Wayback Machine - https://web.archive.org/web/20220327043020/https://github.com/ionic-team/stencil/issues/2969
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

Prerender flag doesn't work with Axios #2969

Open
cptflammin opened this issue Jul 19, 2021 · 9 comments
Open

Prerender flag doesn't work with Axios #2969

cptflammin opened this issue Jul 19, 2021 · 9 comments
Labels
Good First Issue Reply Received Resolution: Needs Investigation

Comments

@cptflammin
Copy link

@cptflammin cptflammin commented Jul 19, 2021

As stated here - but wrongly closed - prerendering fails when using Axios

Problem is that some libraries like AWS AmplifyJS use Axios under the hood.

@adamdbradley Any idea how to solve this serious issue?

@ionitron-bot ionitron-bot bot added the triage label Jul 19, 2021
@splitinfinities splitinfinities added Feature: Prerendering/SSG Repro: No labels Aug 10, 2021
@ionitron-bot ionitron-bot bot removed the triage label Aug 10, 2021
@splitinfinities
Copy link
Contributor

@splitinfinities splitinfinities commented Aug 10, 2021

Hey there, thank you for the patience getting back to you. The new team is getting started and we're working through the backlog now.

Can you help us make a reproduction case? This would help us to identify and solve the problem sooner rather than later.

Thank you!

@tricki
Copy link
Contributor

@tricki tricki commented Sep 6, 2021

There is a repro case in the original issue: https://github.com/xvs32x/port-stencil

I'm having the same problem with Stencil 2.8.0 and axios 0.21.3

@tricki
Copy link
Contributor

@tricki tricki commented Sep 6, 2021

I did some debugging and the problem seems to be that MockAnchorElement objects don't have a pathname property:

var urlParsingNode = document.createElement('a');
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
urlParsingNode.pathname === undefined

Definition of MockAnchorElement:

class MockAnchorElement extends MockHTMLElement {
  constructor(ownerDocument) {
    super(ownerDocument, 'a');
  }
  get href() {
    return fullUrl(this, 'href');
  }
  set href(value) {
    this.setAttribute('href', value);
  }
}

@splitinfinities Is there any way we can extend MockAnchorElement?

@splitinfinities
Copy link
Contributor

@splitinfinities splitinfinities commented Sep 7, 2021

@tricki Awesome investigation! Since that's in the definition we can add it yes, just link how href is being modified. I can't say when we can prioritize this, but if you do happen to add a PR we can get closer to getting it in.

@splitinfinities splitinfinities added Bug Good First Issue Reply Received and removed Repro: No labels Sep 7, 2021
@tricki
Copy link
Contributor

@tricki tricki commented Oct 2, 2021

I created a pull request for adding pathname. Not sure if it's how you meant it @splitinfinities, if not just ping me.

Unfortunately Axios will still not work during prerender because XMLHttpRequest doesn't seem to be mocked, but at least the build won't break anymore (Before it broke even if you only imported Axios without calling any method).

In my case I replaced the Axios call with a fetch during prerender. Not ideal but it works.

if (Build.isServer) {
  fetch('...');
} else {
  Axios.get('...');
}

@squelix
Copy link

@squelix squelix commented Oct 27, 2021

It should work with Axios because of this code in the default.js. But in the dist-hydrate-script build, the final code in the hydrate/index.js is

function getDefaultAdapter() {
  var adapter;
  if (typeof XMLHttpRequest !== 'undefined') {
    // For browsers use XHR adapter
    adapter = xhr;
  } else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
    // For node use HTTP adapter
    adapter = xhr;
  }
  return adapter;
}

So in my node server i get the error XMLHttpRequest is not defined.
So i think the rollup configuration must be updated.

@tricki
Copy link
Contributor

@tricki tricki commented Nov 9, 2021

Good catch. It might be related to axios/axios#2731

@cptflammin
Copy link
Author

@cptflammin cptflammin commented Nov 9, 2021

How could the rollup configuration be updated then ? Could the stencil team give a hand on this?

@tricki
Copy link
Contributor

@tricki tricki commented Dec 4, 2021

I just did a quick test and found a way to work around this. It's not a real solution but might help someone else debug the issue.

What I did:
Based on this comment in the axios issue I linked to before I tried simply removing that line in node_modules/axios/package.json. This lead to a bunch of errors, including:

[ ERROR ]  Node Polyfills Required
           For the import "https" to be bundled from ./node_modules/axios/lib/adapters/http.js, ensure the
           "rollup-plugin-node-polyfills" plugin is installed and added to the stencil config plugins (client). Please
           see the bundling docs for more information. Further information: https://stenciljs.com/docs/module-bundling

So I installed and added rollup-plugin-node-polyfills and the issue was fixed.

@rwaskiewicz rwaskiewicz added Resolution: Needs Investigation and removed Feature: Prerendering/SSG labels Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Reply Received Resolution: Needs Investigation
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants