This commit is contained in:
38
lib/services/create-services.server.ts
Normal file
38
lib/services/create-services.server.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import 'server-only';
|
||||||
|
|
||||||
|
import { AppServices } from './app-services';
|
||||||
|
import { NoopAnalyticsService } from './analytics/noop-analytics-service';
|
||||||
|
import { UmamiAnalyticsService } from './analytics/umami-analytics-service';
|
||||||
|
import { MemoryCacheService } from './cache/memory-cache-service';
|
||||||
|
import { RedisCacheService } from './cache/redis-cache-service';
|
||||||
|
import { GlitchtipErrorReportingService } from './errors/glitchtip-error-reporting-service';
|
||||||
|
import { NoopErrorReportingService } from './errors/noop-error-reporting-service';
|
||||||
|
|
||||||
|
let singleton: AppServices | undefined;
|
||||||
|
|
||||||
|
export function getServerAppServices(): AppServices {
|
||||||
|
if (singleton) return singleton;
|
||||||
|
|
||||||
|
const umamiEnabled = Boolean(process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID);
|
||||||
|
const sentryEnabled = Boolean(process.env.SENTRY_DSN);
|
||||||
|
|
||||||
|
const analytics = umamiEnabled
|
||||||
|
? new UmamiAnalyticsService({ enabled: true })
|
||||||
|
: new NoopAnalyticsService();
|
||||||
|
|
||||||
|
const errors = sentryEnabled
|
||||||
|
? new GlitchtipErrorReportingService({ enabled: true })
|
||||||
|
: new NoopErrorReportingService();
|
||||||
|
|
||||||
|
const redisUrl = process.env.REDIS_URL;
|
||||||
|
const cache = redisUrl
|
||||||
|
? new RedisCacheService({
|
||||||
|
url: redisUrl,
|
||||||
|
keyPrefix: process.env.REDIS_KEY_PREFIX ?? 'klz:',
|
||||||
|
})
|
||||||
|
: new MemoryCacheService();
|
||||||
|
|
||||||
|
singleton = new AppServices(analytics, errors, cache);
|
||||||
|
return singleton;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,7 +2,6 @@ import { AppServices } from './app-services';
|
|||||||
import { NoopAnalyticsService } from './analytics/noop-analytics-service';
|
import { NoopAnalyticsService } from './analytics/noop-analytics-service';
|
||||||
import { UmamiAnalyticsService } from './analytics/umami-analytics-service';
|
import { UmamiAnalyticsService } from './analytics/umami-analytics-service';
|
||||||
import { MemoryCacheService } from './cache/memory-cache-service';
|
import { MemoryCacheService } from './cache/memory-cache-service';
|
||||||
import { RedisCacheService } from './cache/redis-cache-service';
|
|
||||||
import { GlitchtipErrorReportingService } from './errors/glitchtip-error-reporting-service';
|
import { GlitchtipErrorReportingService } from './errors/glitchtip-error-reporting-service';
|
||||||
import { NoopErrorReportingService } from './errors/noop-error-reporting-service';
|
import { NoopErrorReportingService } from './errors/noop-error-reporting-service';
|
||||||
|
|
||||||
@@ -31,13 +30,10 @@ export function getAppServices(): AppServices {
|
|||||||
? new GlitchtipErrorReportingService({ enabled: true })
|
? new GlitchtipErrorReportingService({ enabled: true })
|
||||||
: new NoopErrorReportingService();
|
: new NoopErrorReportingService();
|
||||||
|
|
||||||
const redisUrl = process.env.REDIS_URL;
|
// IMPORTANT: This module is imported by client components.
|
||||||
const cache = redisUrl
|
// Do not import Node-only modules (like the `redis` client) here.
|
||||||
? new RedisCacheService({
|
// Use [`getServerAppServices()`](lib/services/create-services.server.ts:1) on the server.
|
||||||
url: redisUrl,
|
const cache = new MemoryCacheService();
|
||||||
keyPrefix: process.env.REDIS_KEY_PREFIX ?? 'klz:',
|
|
||||||
})
|
|
||||||
: new MemoryCacheService();
|
|
||||||
|
|
||||||
singleton = new AppServices(analytics, errors, cache);
|
singleton = new AppServices(analytics, errors, cache);
|
||||||
return singleton;
|
return singleton;
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user