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.

Change title on each page on route

See original GitHub issue

Description

  • Type: Question

Question

How can I change the title on each page for universal app. So for example I would like About.jsx to have About | React Go title, Dashboard.jsx to have Dashboard | React Go and so onโ€ฆ I would also like to make use of titleTemplate

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:28 (18 by maintainers)

github_iconTop GitHub Comments

3reactions
choonkendingcommented, Oct 6, 2016

Yes it will, you will have to specify it. Look at the comparision here, still some stuff to clean up for production - but it works on dev.

Need to find a bunker somewhere and finish this off next week (after Tuesday).

3reactions
ZeroChocommented, Aug 23, 2016

Meta.jsx is not related with client side title. So if you want to use Helmet in client-side, you should make Meta.jsx totally different from current Meta.jsx

For example meta.jsx in my blog looks like below.

export default (routes, state, params) => {
  let title;
  const post = state.posting.post;
  if (routes && routes[1]) {
    switch (routes[1].path) {
      case '/category/:category/post/:id':
        if (post) {
          const filteredContent = filterTag(post.content).substring(0, 150);
          title = `${post.category} ${post.title}`;
          replaceMeta(config.meta, [
            { name: 'description', content: filteredContent },
            { property: 'og:title', content: title },
            { property: 'og:description', content: filteredContent },
            { property: 'og:type', content: 'article' },
            { property: 'og:url', content: `${url}/category/${post.category}/post/${post._id}` },
            { name: 'twitter:title', content: title },
            { name: 'twitter:description', content: filteredContent },
          ]);
        }
        break;
      case '/category/:category':
        title = params.category;
        replaceMeta(config.meta, [
          { property: 'og:title', content: title },
          { property: 'og:url', content: `${url}/category/${title}` },
          { property: 'og:type', content: 'website' },
          { name: 'twitter:title', content: title },
          { name: 'description', content: `${title} ๊ด€๋ จ ๊ธ€ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค` },
          { property: 'og:description', content: `${title} ๊ด€๋ จ ๊ธ€ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค` },
          { name: 'twitter:description', content: `${title} ๊ด€๋ จ ๊ธ€ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค` },
        ]);
        break;
      case '/settings':
        title = '์„ค์ •';
        replaceMeta(config.meta, [
          { property: 'og:title', content: title },
          { property: 'og:url', content: `${url}/settings` },
          { property: 'og:type', content: 'website' },
          { name: 'twitter:title', content: title },
          { name: 'description', content: '์„ค์ • ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
          { property: 'og:description', content: '์„ค์ • ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
          { name: 'twitter:description', content: '์„ค์ • ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
        ]);
        break;
      case '/login':
        title = '๋กœ๊ทธ์ธ';
        replaceMeta(config.meta, [
          { property: 'og:title', content: title },
          { property: 'og:url', content: `${url}/login` },
          { property: 'og:type', content: 'website' },
          { name: 'twitter:title', content: title },
          { name: 'description', content: '๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
          { property: 'og:description', content: '๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
          { name: 'twitter:description', content: '๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
        ]);
        break;
      case '/join':
        title = 'ํšŒ์›๊ฐ€์ž…';
        replaceMeta(config.meta, [
          { property: 'og:title', content: title },
          { property: 'og:url', content: `${url}/join` },
          { property: 'og:type', content: 'website' },
          { name: 'twitter:title', content: title },
          { name: 'description', content: 'ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
          { property: 'og:description', content: 'ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
          { name: 'twitter:description', content: 'ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.' },
        ]);
        break;
      case '/tag/:name':
        title = `${params.name} ํƒœ๊ทธ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ`;
        replaceMeta(config.meta, [
          { property: 'og:title', content: title },
          { property: 'og:url', content: `${url}/tag/${params.name}` },
          { property: 'og:type', content: 'website' },
          { name: 'twitter:title', content: title },
          { name: 'description', content: `${params.name} ํƒœ๊ทธ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.` },
          { property: 'og:description', content: `${params.name} ํƒœ๊ทธ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.` },
          { name: 'twitter:description', content: `${params.name} ํƒœ๊ทธ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํŽ˜์ด์ง€์ž…๋‹ˆ๋‹ค.` },
        ]);
        break;
      case '/portfolio':
        title = 'ํฌํŠธํด๋ฆฌ์˜ค';
        break;
      default:
        replaceMeta(config.meta, [
          { name: 'description', content: description },
          { property: 'og:url', content: url },
          { property: 'og:type', content: 'website' },
          { property: 'og:description', content: description },
          { property: 'og:title', content: config.defaultTitle },
          { name: 'twitter:title', content: config.defaultTitle },
          { name: 'twitter:description', content: description },
        ]);
        break;
    }
  }
  const Meta = () => (
    <Helmet
      htmlAttributes={config.htmlAttributes}
      defaultTitle={title ? '' : config.defaultTitle}
      titleTemplate={config.titleTemplate}
      title={title || 'Main'}
      meta={config.meta}
      link={config.link}
    />
  );
  renderToString(<Meta />);
  return Helmet.rewind();
};

and I use it in app/server.jsx like below

preRenderMiddleware(store.dispatch, props.components, props.params)
        .then(() => {
          const initialState = store.getState();
          const componentHTML = (
            <Provider store={store} key="provider">
              <RouterContext {...props} />
            </Provider>
          );
          const header = makeHeader(props.routes, initialState, props.params); // this is the part which import Meta.jsx
          const finalHTML = `<!DOCTYPE html>${renderToString(
            <Layout header={header} content={componentHTML} initialState={initialState} />
          )}`;
          res.status(200).send(finalHTML);
        })
        .catch((renderError) => {
          console.error('renderError', renderError);
          res.status(500).json(renderError);
        });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting Page Titles Natively With The Angular Router
First, you would use the data property in the Route object with a title key to set the title for the page. import...
Read more >
How to change page title with routing in Angular application?
You have a TitleService in Angular 5. Inject it in your component's constructor, and use the setTitle() method. import {Title} fromย ...
Read more >
Dynamic Page Title based on Route in Angular
The trick to changing the title is to know when the route changes. This we can do by listening to the NavigationEnd event....
Read more >
Dynamically Change Page Title for each Route in Angular
In this article, I'll show you how to dynamically change page title of an Angular app based on Route configuration.
Read more >
Dynamic Page Titles in Angular - Bits and Pieces
Learn how to change your page title (in Angular) using TitleService ... title for each page at one place with subscribing to angular...
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