Comments (8)
👋 I'll take this over and add to @leerob's example making a more complete on in this repo! Should get released within 5-10 days.
from examples.
I'm seconding this! I see this mentioned as a possible use-case, but honestly, I fail to see how it could work, even after looking at the implementation inside Next.js.
from examples.
@rbrcsk I also came here looking for an example, maybe I can give an idea of how it would or could work.
The middleware allows you to "intercept" each request and perform some logic. In our case, this logic could store in a database information about the request (e.g. path, user agent, time, IP address), this way you would have statistics about each request made to your server.
The logic could also just call the API of an external web analytics service (e.g. GoatCounter) so you don't have to handle the stats yourself.
I guess it would be better to make this logic asynchronous so as not to impact request time.
from examples.
Thanks @nyg! I've managed to go that far, my problem is, I'd need to get access to the response that is sent, and that seems like a no-go unfortunately.
from examples.
@rbrcsk Indeed, I don't think you can get the response just before it is sent back to the browser. Can I ask what info do you need from the response?
from examples.
I tried to use firebase to do serverside analytics but I didn't succeed.
Here's my code: (of course we can't use firebase-admin because it depends on fs module)
import type { NextFetchEvent, NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { ref, runTransaction } from 'firebase/database';
import { initializeApp } from 'firebase/app';
import { getDatabase } from 'firebase/database';
const app = initializeApp(/* my firebaseConfig */);
const db = getDatabase(app);
const logView = async (slug) => {
const { snapshot } = await runTransaction(ref(db, 'views/' + slug), (currentViews) =>
currentViews === null ? 1 : currentViews + 1
);
return snapshot.val();
};
export function middleware(req: NextRequest, ev: NextFetchEvent) {
const { pathname } = req.nextUrl;
if (!/^\/static/.test(pathname)) {
const noSlashPathName = pathname.replace(/^\//, '').replace(/\//g, '-');
logView(noSlashPathName).catch(console.log);
}
return NextResponse.next();
}
if I await
the function logView
, the request hangs forever
there is no error logged in the console
from examples.
I have a WIP version going here for Next.js leerob/leerob.io#431
from examples.
Closing this as we merged the example:
Code: https://github.com/vercel/examples/tree/main/edge-functions/analytics
Deployed explainer: https://edge-functions-analytics.vercel.app/
from examples.
Related Issues (20)
- nextjs-flask example broken HOT 6
- Clarification on the "@acme-" Prefix in Multi-Zones Packages
- postgres-pgvector demo site not working HOT 1
- Web3 Authentication example is insecure HOT 1
- Next.js + Flask fails in production HOT 2
- Microfrontends example: Module not found: Can't resolve './example.module.css'
- feat(storybook): introduce to `/solution/monorepo`
- Bug: Wrong header in `https://app-dir-i18n.vercel.app/` HOT 1
- nextjs-flask: The flask api endpoint is not working. HOT 2
- Your location is undefined HOT 5
- PostgreSQL on docker and Next.js locally Error: connect ECONNREFUSED ::1:443 HOT 1
- Node.js Serverless Function template doesn't work (gives a 404) HOT 2
- Update deployment GitHub workflows
- bug
- Domain API Example broken
- Flask3 example doesn't work with additional dependencies
- Sonic example doesn't work
- https://flask2-python-template.vercel.app/ HOT 2
- postgres-drizzle got slow
- How to run Flask WSGI in Multithreaded Mode
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.