Skip to content

Commit

Permalink
Merge pull request #50 from nuxt-community/bridge
Browse files Browse the repository at this point in the history
fix: support nuxt bridge
  • Loading branch information
aldarund authored Jul 13, 2022
2 parents 09af494 + cac29df commit 561e89a
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 168 deletions.
10 changes: 9 additions & 1 deletion lib/appinsights.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import * as applicationinsights from 'applicationinsights'

export default function (ctx, inject) {
// Inject AppInsights to the context as $appInsights
inject('appInsights', process.appInsights || {})
const config = <%= serialize(options.config) %>
const appInsightsServer = applicationinsights.setup(<%= serialize(options.instrumentationKey) %>)
applicationinsights.defaultClient.config = config
if (optionsServer.initialize) {
appInsightsServer.start()
}
inject('appInsights', applicationinsights || {})
}
12 changes: 9 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,28 @@ module.exports = function appInsights (moduleOptions) {
const optionsServer = deepMerge.all([options, privateRuntimeConfig])

const appInsightsServer = AppInsights.setup(optionsServer.instrumentationKey)
const config = { ...AppInsights.defaultClient.config, ...optionsServer.serverConfig }

AppInsights.defaultClient.config = { ...AppInsights.defaultClient.config, ...optionsServer.serverConfig }
AppInsights.defaultClient.config = config
// Initialize AppInsights
if (optionsServer.initialize) {
appInsightsServer.start()
}
const appInsightsClient = AppInsights.defaultClient

process.appInsights = appInsightsClient
logger.success('Started logging errors to AppInsights')

this.addPlugin({
src: path.resolve(__dirname, 'appinsights.server.js'),
fileName: 'appinsights.server.js',
mode: 'server'
mode: 'server',
options: {
instrumentationKey: optionsServer.instrumentationKey,
config,
initialize: optionsServer.initialize
}
})

this.nuxt.hook('render:setupMiddleware', app => app.use(requestHandler(appInsightsClient)))
this.nuxt.hook('render:errorMiddleware', app => app.use(errorHandler(appInsightsClient)))
this.nuxt.hook('generate:routeFailed', ({ errors }) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"nuxt": "node --inspect=0.0.0.0 node_modules/nuxt-edge/bin/nuxt test/fixture"
},
"dependencies": {
"applicationinsights": "^2.1.2",
"@microsoft/applicationinsights-web": "^2.6.3",
"applicationinsights": "^2.3.3",
"@microsoft/applicationinsights-web": "^2.8.5",
"consola": "^2.15.0",
"deepmerge": "^4.2.2"
},
Expand Down
Loading

0 comments on commit 561e89a

Please sign in to comment.