arrow_back Back to Tickets

Ticket #e24037

open

Problem

improve my post fetching process to increase the speed.

Creator

marcus

Priority

5

Urgent

not_urgent

Important

important

Project Name

zfrika

Notes

To move this to the Server-Side, we should transition from sessionStorage (which only the browser can see) to Cookies or Session Objects (which the server can see). This allows your Node.js server to redirect the user instantly without showing a "please wait" page at all. 1. The Controller Logic (Express.js) Instead of rendering an HTML page that runs a script, your server checks the user's status and sends a 302 Redirect header immediately. JavaScript // In your router/controller app.get('/fetch-my-posts', (req, res) => { // 1. Check for token in cookies or session const dataToken = req.cookies.datatoken || req.session.datatoken; if (dataToken && dataToken !== 'null') { // 2. Direct redirect (Server-to-Server) // This is much faster as it skips the 'Loading' page download return res.redirect(`/zfrikadev/userposts/${dataToken}`); } else { // 3. Fallback if they aren't logged in return res.redirect('/zfrikadev/login'); } }); 2. Comparison: Client-Side vs. Ser Feature Client-Side (Your current way) Server-Side (Recommended) Speed Slow (Downloads page, then redirects) Fast (Instant header response) User Experience Shows "Please Wait" screen No flickering; seamless transition SEO Harder for bots to follow Preferred by Search Engines Security Token is exposed in JS Token can be hidden in HttpOnly cookies 3. How to set the Cookie (During Login) For this to work, when the user logs in, you must save the token in a Cookie so the server can read it later. In your Login POST route: JavaScript // When user logs in successfully res.cookie('datatoken', userToken, { maxAge: 900000, // Expires in 15 mins httpOnly: true, // Prevents JS from stealing the token (Secure!) path: '/zfrikadev' }); res.redirect('/zfrikadev/home'); 4. Why this is the "Pro" Move By using the Server-Side approach, you eliminate the "Redirect Loop" and "Flash of Content" issues. The user clicks a link, and the next thing they see is their posts. It makes zfrika.com feel like a high-end, snappy application rather than a series of connected scripts. Would you like me to help you update your Login route to start using Cookies instead of sessionStorage? To move from sessionStorage to Server-Side Cookies, we need to update two parts of your app: the Login Route (to save the cookie) and your Protected Routes (to read the cookie). 1. Update the Login Route In your authController.js (or wherever your login logic lives), you need to send a Set-Cookie header to the browser once the user is authenticated. JavaScript // Inside your login POST function const userToken = "generated_token_here"; // Your existing token logic // Set a cookie that the server can read on every request res.cookie('datatoken', userToken, { httpOnly: true, // Security: prevents client-side JS from reading it secure: false, // Set to true if using HTTPS maxAge: 3600000, // 1 hour in milliseconds path: '/zfrikadev' // Ensures cookie is sent only to your app path }); // Now redirect to home - no more "Fetching Posts" page needed! res.redirect('/zfrikadev/home'); 2. Update the "Fetch Posts" Route Now, you can delete that HTML loading page entirely. Your route will now look like this: JavaScript app.get('/zfrikadev/my-posts', (req, res) => { // The server looks at the cookies sent by the browser automatically const dataToken = req.cookies.datatoken; if (dataToken) { // Redirect directly to the posts page res.redirect(`/zfrikadev/userposts/${dataToken}`); } else { // No cookie? Send to login with a return path res.redirect('/zfrikadev/login?returnTo=/zfrikadev/my-posts'); } }); 3. Benefits of this approach Zero Latency: The user doesn't have to download an intermediate HTML page just to run a script. The redirect happens at the network level. Better Security: By using httpOnly: true, you protect your datatoken from XSS attacks. Malicious scripts can't use sessionStorage.getItem(). Persistence: If the user closes the tab and comes back, the cookie is still there (sessionStorage wipes when the tab is closed). 4. Required Package To make req.cookies work in Express, make sure you have cookie-parser installed and initialized in your app.js: Terminal: Bash npm install cookie-parser app.js: JavaScript const cookieParser = require('cookie-parser'); app.use(cookieParser()); Summary Checklist: Install cookie-parser in your project. Update Login Route to use res.cookie(). Delete the "please wait" HTML file and replace it with a direct res.redirect() in your backend. Would you like me to show you how to update your logout route so it clears the cookie when the user leaves?

Ticket Information

Ticket ID: 695c4f29df2473a6c8e24037
Date Initiated: 1/5/2026, 3:51:00 PM
Status: open
Urgent: not_urgent
Important: important
Created: 1/5/2026, 3:54:17 PM
Last Updated: 1/5/2026, 3:54:17 PM