question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Polly does not respect <base> tag

See original GitHub issue

Prerequisites

A website running under a certain path e.g. “http://localhost:4200/some/feature

Description

XHR requests made to a relative url, e.g. “rest/my-api” are altered by PollyJS to contain parts of the browser url. In this example “/some/rest/my-api”.

Config

      Polly.register(XHRAdapter);
      Polly.register(PersisterStub);

      const polly = new Polly('mock', {
        adapters: ['xhr'],
        persister: 'PersisterStub'
      });

      const { server } = polly;

      server.get().passthrough();

Dependencies

{
    "@pollyjs/adapter-xhr": "~4.0.2",
    "@pollyjs/core": "~4.0.2",
    "@pollyjs/persister-rest": "~4.0.2",
}

Relevant Links

https://github.com/nbfr/pollyjs-dream-app

You can run the app with “npm run start” to run without PollyJS or “npm run start-mock” to run with PollyJS. Then open http://localhost:4200/some/feature.

Then check the network traffic. The client will send a request to “rest/my-api” which is changed by PollyJS to “some/rest/my-api”.

Environment

Node.JS v10.16.3 win32 10.0.1832 npm 6.9.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
nbfrcommented, Mar 23, 2020

@jasonmit : Thank you for quick feedback and the work around. For now I decided to extend the existing XHRAdapter.

class ProjectXHRAdapter extends XHRAdapter {
  static get type() {
    return 'adapter';
  }

  onConnect() {
    const basePath = new URL(document.baseURI).pathname;
    super.onConnect();
    const onCreate = (this as any).xhr.onCreate;
    (this as any).xhr.onCreate = xhrReq => {
      onCreate(xhrReq);
      const send = xhrReq.send;
      xhrReq.send = body => {
        if (!xhrReq.url.startsWith('/') && !xhrReq.url.startsWith('http://') && !xhrReq.url.startsWith('https://')) {
          xhrReq.url = basePath + xhrReq.url;
        }
        send(body);
      };
    };
  }
}
1reaction
jasonmitcommented, Mar 22, 2020

Okay, I believe this is caused by Polly not respecting the <base href="/"> in your template when building the request URL.

For now, a work around like this should unblock you:

import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';

function baseifyUrl(url: string) {
  let base = document.querySelector('base');

  if (base) {
    return base.getAttribute('href') + url;
  }

  return url;
}

@Component({
  selector: 'app-feature',
  templateUrl: './feature.component.html'
})
export class FeatureComponent implements OnInit {

  public requestPath = baseifyUrl('rest/my-api');

  constructor(private httpClient: HttpClient) {
  }

  ngOnInit(): void {
    this.httpClient.get(this.requestPath).subscribe((result) => console.log(result));
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

not respect <base > tag · Issue #1426 · vuejs/vue-router - GitHub
What is actually happening? vue-router can't parse base url. This example is from the html spec about base tag https://html.spec ...
Read more >
Why base tag does not work for relative paths? - Stack Overflow
Something doesn't make sense here. Although paths starting with / are absolute, URLs containing such paths are actually called relative URLs.
Read more >
Best practices with HttpClient and Retry Policies with Polly in ...
Create a Retry Policy from the base PolicyBuilder. Here we are! let's see how we are using configuration that we defined before to...
Read more >
25090 - BASE tag ignored if original page URL contains http ...
the original source URL contains the BASE HREF URL as a substring. ... it works fine, no bug. ... then it's buggy and...
Read more >
Essentials - Julia Documentation
The behaviors of Base and standard libraries are stable as defined in SemVer only if they are documented; i.e., included in the Julia...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found