Dynamic Title Problem
Answered
Large oak-apple gall posted this in #help-forum
Large oak-apple gallOP
Hi;
I am trying to make dynamic title. I have GlobalContext and it has title useState in it. I update the title state every time the page changes and just call the title state in the NavigationMenu component. But something strange happens during page change. It will be more explanatory to show it with video.
The text "localhost:3000" appears in the title when changing the page. How can I fix this?
Here is the simple view of the code I have
I also tried this but got the same result.
I am trying to make dynamic title. I have GlobalContext and it has title useState in it. I update the title state every time the page changes and just call the title state in the NavigationMenu component. But something strange happens during page change. It will be more explanatory to show it with video.
The text "localhost:3000" appears in the title when changing the page. How can I fix this?
Here is the simple view of the code I have
// components/Nav.jsx
function Nav({ children }) {
...
const { title } = useAppContext();
...
return (
...
<Head>
<title>{title}</title>
</Head>
...
)
}
// pages/index.jsx
function Home() {
const { setTitle, titleTradeMark } = useAppContext();
useEffect(() => {
setTitle("Main Page - " + titleTradeMark);
});
...
}
// pages/users/index.jsx
function Users() {
const { setTitle, titleTradeMark } = useAppContext();
useEffect(() => {
setTitle("Users - " + titleTradeMark);
});
...
}I also tried this but got the same result.
// pages/index.jsx
function Home() {
...
return (
<Head>
<title>Main Page - New Mark</title>
</Head>
)
...
}
// pages/users/index.jsx
function Users() {
...
return (
<Head>
<title>Users - New Mark</title>
</Head>
)
...
}51 Replies
your second code looks fine to me, you need to show me more code to see what's wrong
@joulev your second code looks fine to me, you need to show me more code to see what's wrong
Large oak-apple gallOP
let me change code to second code quickly wait a sec
@joulev your second code looks fine to me, you need to show me more code to see what's wrong
Large oak-apple gallOP
pages/users/index.jsx
pages/index.jsx
does the problem show up in prod mode (npm run build && npm start)
Large oak-apple gallOP
nope. in the dev mode (npm run dev)
Should I try it in prod mode?
Large oak-apple gallOP
still same in prod mode
i can't reproduce it even in dev mode. can you give me a minimal reproduction repository https://stackoverflow.com/help/minimal-reproducible-example
@joulev i can't reproduce it even in dev mode. can you give me a minimal reproduction repository <https://stackoverflow.com/help/minimal-reproducible-example>
Large oak-apple gallOP
I've never used github repository before. We can connect AnyDesk if you want?
This will be faster.
no. i prefer github though codesandbox and similar works too. you can also paste the content of all files in your minimal repro repository here if you want
@joulev no. i prefer github though codesandbox and similar works too. you can also paste the content of all files in your **minimal** repro repository here if you want
Large oak-apple gallOP
What do you mean by minimal? I mean, what should I exclude to keep the code short?
imports, some functions i think. What else could it be?
@Large oak-apple gall What do you mean by minimal? I mean, what should I exclude to keep the code short?
see this link on how to make one https://stackoverflow.com/help/minimal-reproducible-example. basically a very small nextjs app, with all your business logic removed, that can still reproduce the above problem
as small as possible
Large oak-apple gallOP
hmm
let me try to do
Is it okay if I create codesandbox?
any way by which i can access a nextjs app is fine
Large oak-apple gallOP
I guess I didn't do exactly what you wanted, but the problem might be the useEffect part in the _app file.
Head title doesn't seem to work in Codesandbox.
If that doesn't help, I'll push the whole project to a github repo for you.
Large oak-apple gallOP
yeah i found it
The error is happening because of this code
// pages/_app.js
useEffect(() => {
// on initial load - run auth check
authCheck(router.asPath);
// on route change start - hide page content by setting authorized to false
const hideContent = () => setAuthorized(false);
router.events.on("routeChangeStart", hideContent);
// on route change complete - run auth check
router.events.on("routeChangeComplete", authCheck);
// unsubscribe from events in useEffect return function
return () => {
router.events.off("routeChangeStart", hideContent);
router.events.off("routeChangeComplete", authCheck);
};
}, []);
function authCheck(url) {
const publicPaths = ["/login"];
const path = url.split("?")[0];
if (!userService.userValue && !publicPaths.includes(path)) {
setAuthorized(false);
router.push({
pathname: "/login",
query: { returnUrl: router.asPath },
});
} else {
setAuthorized(true);
}
}How can I fix this, I have no idea. I got these codes from a example project. https://jasonwatmore.com/next-js-13-mysql-user-registration-and-login-tutorial-with-example-app#_app-js
@joulev i can't reproduce it
Large oak-apple gallOP
I found what caused weirdness, but I have no clue how to solve it.
refactor it so that the
Head tag is intact regardless of authorized valueLarge oak-apple gallOP
I guess yes
@joulev refactor it so that the `Head` tag is intact regardless of `authorized` value
Large oak-apple gallOP
I can do that. But because of that code some strange things happen. It's like the theme is reapplied every time I switch pages. Any idea how I can make this code more effective?
Large oak-apple gallOP
Look at this
You see the blink effect, right?
all i see is that you are hiding content on route change and show them again after some check is done -> this check is very fast so that caused a very fast blink
Large oak-apple gallOP
Definitely
Any idea how can I optimize this?
by not hiding your page content?
Answer
user is logged in when they access
/dashboard, you don't need to check the auth again when they navigate to /settingsonly need to check the auth onload
and during login/logout
Large oak-apple gallOP
Ok. Thanks for everything. Is there anything I can do for you? It's like giving points idk.
🤷â€â™‚ï¸
kek, that one is intended for people who promote their apps in wrong places
it happens more often than you think
thanks for the answer mark