Simple query takes forever to load data from DB, works fine in new tab but not in same tab
Unanswered
ZgazenaMacka posted this in #help-forum
Hello. I have a problem that I can't think of what's causing it.
I have a simple MongoDB query, it works fine and fast if the page link is open in new tab but takes forever if the link is opened in the same tab via Link component
To test it out you can visit this page https://www.groblje-horora.com/tags it's a list of tags, and on click it goes to single tag page which shows all posts with that tag.
When you just clik on one of tags a new page is open in same tab and takes forewer to query the data, almost 28000ms (screenshot 1)
and if you try to open the same or any other tag in a new tab it will query almost instantly
I have a simple MongoDB query, it works fine and fast if the page link is open in new tab but takes forever if the link is opened in the same tab via Link component
To test it out you can visit this page https://www.groblje-horora.com/tags it's a list of tags, and on click it goes to single tag page which shows all posts with that tag.
When you just clik on one of tags a new page is open in same tab and takes forewer to query the data, almost 28000ms (screenshot 1)
and if you try to open the same or any other tag in a new tab it will query almost instantly
4 Replies
here's the code for single tag page:
and here's the API route code for single tag
import { dbConnect } from "@/lib/mongo/dbConnect";
import reviewModel from "@/lib/mongo/models/reviewModel";
import { NextResponse } from "next/server"
export const GET = async (request, { params }) => {
dbConnect()
const page = request.nextUrl.searchParams.get('page');
const perPage = request.nextUrl.searchParams.get('perPage');
const { tag } = params;
const skip = page === "undefined" ? 0 : ((page - 1) * parseInt(perPage));
const query = {
"movies.tags.tagValue": tag
};
const reviews = await reviewModel.find(query)
.skip(parseInt(skip))
.limit(parseInt(perPage))
.sort({ "createdAt": -1 })
.exec();
const reviewsCount = await reviewModel.countDocuments(query);
const totalPages = Math.ceil(reviewsCount / perPage);
if (!reviews || reviews.length === 0) {
return new NextResponse(JSON.stringify({ error: 'No reviews with that tag' }), {
status: 404
})
}
return new NextResponse(JSON.stringify({
reviews,
reviewsCount,
totalPages,
}), {
status: 200
})
}
import { dbConnect } from "@/lib/mongo/dbConnect";
import reviewModel from "@/lib/mongo/models/reviewModel";
import { NextResponse } from "next/server"
export const GET = async (request, { params }) => {
dbConnect()
const page = request.nextUrl.searchParams.get('page');
const perPage = request.nextUrl.searchParams.get('perPage');
const { tag } = params;
const skip = page === "undefined" ? 0 : ((page - 1) * parseInt(perPage));
const query = {
"movies.tags.tagValue": tag
};
const reviews = await reviewModel.find(query)
.skip(parseInt(skip))
.limit(parseInt(perPage))
.sort({ "createdAt": -1 })
.exec();
const reviewsCount = await reviewModel.countDocuments(query);
const totalPages = Math.ceil(reviewsCount / perPage);
if (!reviews || reviews.length === 0) {
return new NextResponse(JSON.stringify({ error: 'No reviews with that tag' }), {
status: 404
})
}
return new NextResponse(JSON.stringify({
reviews,
reviewsCount,
totalPages,
}), {
status: 200
})
}
I just noticed that loading.js is causing this problem.
I have loading.js at the root folder and it works fine, but once I visit /tags/[tag] I see no loading, so I added loading.js in /tags/[tag] folder
I have loading.js at the root folder and it works fine, but once I visit /tags/[tag] I see no loading, so I added loading.js in /tags/[tag] folder