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.

Links to homepage still point to WP host after node processing

See original GitHub issue

Hi 👋

I’ve found that urls in links to homepage are not replaced by relative paths on GraphQL node creation. They are still pointing to WP host and not the Gatsby frontend host.

Here’s an example of how links are transformed (I’ll provide endpoint and details at the bottom): gql-gatsby-links

On the image: link to https://wordpress-for-fun.000webhostapp.com/about becomes /about, but https://wordpress-for-fun.000webhostapp.com stays the same.

Possible fix

As I can see, replaceNodeHtmlLinks() here in process-node.js is responsible for checking and replacing links with relative paths in node strings. This RegExp looks for a match:

["']${wpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]+)["']

It assumes that the URL should have a path with 1 or more characters after /, which is what + stands for. So homepage path does not match.

I changed + to * to indicate that there can be 0 or more characters, like this:

["']${wpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]*)["']

and ran gatsby develop after clearing cache. It seems to have fixed the issue on my website (on a much bigger website, not this example) without any negative side effects. I’ve also ran tests for the plugin in the repo and they pass.

I wanted to make a PR, but still I might have missed some downstream effects that I’m not aware of. Please let me know if my fix is OK. 🙂

Steps to reproduce reduced case

Run gatsby develop with this endpoint as source: https://wordpress-for-fun.000webhostapp.com/graphql

Dependencies:

"gatsby": "^2.30.3",
"gatsby-source-wordpress-experimental": "^7.0.3",
"react": "^16.14.0",
"react-dom": "^16.14.0"

Go to GraphiQL playground on Gatsby’s side and query all page nodes with contents.

query {
  allWpPage {
    nodes {
      content
      title
    }
  }
}

Desired result

All links in page content are replaced with relative paths.

Actual result

All links except for links to homepage are replaced with relative paths.

Proposed fix

In steps/source-nodes/create-nodes/process-node.js change line 798:

- `["']${wpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]+)["']`
+ `["']${wpUrl}(?!/wp-content|/wp-admin|/wp-includes)(/[^'"]*)["']`

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TylerBarnescommented, Feb 24, 2021

@karen-sarkisyan please do 😃 that’d be much appreciated! The contributing docs should be enough to get going but let me know if anything doesn’t work.

0reactions
LekoArtscommented, Jul 15, 2022

Hi!

I’m closing this as a stale issue as in the meantime Gatsby 4 and related packages were released. You can check our Framework Version Support Page to see which versions currently receive active support. If this is a feature request, please create a discussion as we moved feature requests from issues to GitHub Discussions.

Please try the mentioned issue on the latest version (using the next tag) and if you still see this problem, open a new bug report. It must include a minimal reproduction.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Moved site, Old links in pages still pointing to old host
I have a site that I moved from one site with an old domain to a new host with an new domain. I...
Read more >
WordPress – Changing the Site URL and Home Settings
Has your wordpress website been broken after moving the files? This article goes over how to fix the issue and make your website...
Read more >
Configure the create-react-app public URL post-build with ...
For a project, I had the requirement of making the public URL of the application configurable via an environment variable that can be...
Read more >
How To Create a Web Server in Node.js with the HTTP Module
It only exits if it encounters an error that causes it to crash and quit, or if we stop the Node.js process running...
Read more >
Common WordPress Errors and Access Logs - Support Center
Please try again.” 13 Error establishing database connection. 14 Allowed memory size of xxx bytes exhausted. 15 ...
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