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.

Include AJAX-fetched data in page title?

See original GitHub issue
  • /: Products listing. Clicking on a product takes you to for instance /product/12.
  • /product/:id: Product detail page.

On the start page I want the page title (document.title) to be: “MyStore”. Easy enough.

On the product page the page title should be for instance: “Rolex watch – Mystore”. When this page is entered, I fetch the product details with AJAX (from /api/v1/products/12 for instance). When the request is done I have access to the product name (“Rolex watch”) and want to put that in the page title. However, redux-first-router seems to only update the page title when the current location changes, not when arbitrary state updates.

Am I missing something?

Just tell me if you need more details 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
faceyspaceycommented, Mar 7, 2018

That’s the most common use case and all many people need

1reaction
faceyspaceycommented, Mar 7, 2018

that’s a fine solution. you can also set document.title manually any time you want on the client, if you want to avoid a large dependency just for a one liner. …for SSR, it’s just a matter of grabbing state.title and putting it in the HTML you spit out. since this isn’t dependent on RFR updating the browser’s document.title when used on the server, state.title will work fine. Essentially you’re using Helmet now just for the client side aspect.

Here’s another way you might like:

let currentTitle = store.getState().title

store.subscribe(() => {
   if (currentTitle !== store.getState().title) {
      document.title = currentTitle = store.getState().title
   }
})

// and:

title: isClient() ? () => undefined : 'title'

that way it still works as expected on the server, and on the client you update the title on every single title change automatically. no extra libs needed. very few extra lines of code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use ajax to get page title for a URL
1 Answer 1 ... var matches = html.match(/(.*?)</title>/);. in your request function . and put,. dataType: "html",. in your ajax request .
Read more >
How do I add meta data and a page title through ajax?
somewhere in your JavaScript. A good place to do this in regards to the tutorial script you're using would be inside the ajaxLoad()...
Read more >
Fetching Data with AJAX and Django
An AJAX request made with fetch will include several headers. We expect the data returned from the view in JSON form, so we...
Read more >
Getting <title> tag from HTML page using $.ajax()
I am using $.ajax() to pull the page and process the <title> tag from the response $(data). For some reason, Opera is able...
Read more >
Using the Fetch API - MDN Web Docs
The fetch specification differs from jQuery.ajax() in the following ... body data type must match "Content-Type" header }); return ...
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