nuxt-matomo icon indicating copy to clipboard operation
nuxt-matomo copied to clipboard

setting route.meta.matomo has no effect with SSR

Open dhritzkiv opened this issue 6 years ago • 4 comments

When running a universal app, setting route.meta.matomo in a middleware has no effect when the page renders first on the server.

Obviously, matomo is unlikely to work on the server, but is there a way to have this module apply route.meta.matomo when the page is evaluated on the client?

dhritzkiv avatar May 24 '19 19:05 dhritzkiv

Sorry for the late response, could you maybe share some code that explains a bit what you are trying to do?

pimlie avatar Jun 07 '19 15:06 pimlie

Yes. Roughly this (only the relevant parts retained)

// middleware that runs for some pages/layouts
export default async (context: Context) => {
	const { store, route } = context;

	// get the current user
	const {viewer} = store.state;

        // this runs, but has no effect when coming from SSR'd page. It only has an effect on subsequent runs on the client
	route.meta.matomo = {
		userId: ["setUserId", viewer.id],
		userTypeVar: ["setCustomVariable", 1, "UserName", viewer.name],
	}

}

Ideally, route.meta.matomo would get serialized going from server -> client, and then rehydrated on the clientside, and passed to Matomo

dhritzkiv avatar Jun 07 '19 16:06 dhritzkiv

I am not really sure that that is something this module should/could support. Eg if your middleware would be executed at page load (but it isnt, see below) then the meta should be re-applied and only vuex state would need to be hydrated.

Unfortunately it seems calling route middlewares on hydration is just not supported, the workaround is to add a client side plugin but I am not sure you have access to the route.meta there. See this topic and the linked comment for more info: https://github.com/nuxt/nuxt.js/issues/2653#issuecomment-390588837

pimlie avatar Jun 07 '19 18:06 pimlie

Related Nuxt rfc: https://github.com/nuxt/rfcs/issues/28

pimlie avatar Jun 09 '19 09:06 pimlie