relay-nextjs
Advanced tools
Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "relay-nextjs", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Use Relay in your Next.js apps!", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,3 +31,3 @@ `relay-nextjs` acts as a bridge between Next.js and Relay. | ||
// lib/client_environment.ts | ||
import { getRleaySerializedState } from 'relay-nextjs'; | ||
import { getRelaySerializedState } from 'relay-nextjs'; | ||
import { withHydrateDatetime } from 'relay-nextjs/date'; | ||
@@ -218,4 +218,2 @@ import { Environment, Network, Store, RecordSource } from 'relay-runtime'; | ||
export default withRelay(UserProfile, UserProfileQuery, { | ||
// This property is optional. | ||
error: MyCustomErrorComponent, | ||
// Fallback to render while the page is loading. | ||
@@ -222,0 +220,0 @@ // This property is optional. |
import type { AppProps } from 'next/app'; | ||
import type { Environment } from 'react-relay'; | ||
import { WiredProps } from './component'; | ||
import { AnyPreloadedQuery } from './types'; | ||
import type { WiredProps } from './component'; | ||
import type { AnyPreloadedQuery } from './types'; | ||
export declare function getWiredProps(pageProps: AppProps['pageProps'], initialPreloadedQuery: AnyPreloadedQuery | null): Partial<WiredProps>; | ||
@@ -6,0 +6,0 @@ export declare function getInitialPreloadedQuery(opts: { |
@@ -8,11 +8,8 @@ "use strict"; | ||
function getWiredProps(pageProps, initialPreloadedQuery) { | ||
var _a, _b, _c; | ||
const errorContext = context_1.getWiredErrorContext(pageProps.__wired_error_context); | ||
var _a, _b; | ||
const serverContext = context_1.getWiredServerContext(pageProps.__wired__server__context); | ||
const clientContext = context_1.getWiredClientContext(pageProps.__wired__client__context); | ||
const statusCode = (_a = errorContext === null || errorContext === void 0 ? void 0 : errorContext.statusCode) !== null && _a !== void 0 ? _a : 200; | ||
const err = errorContext === null || errorContext === void 0 ? void 0 : errorContext.err; | ||
const CSN = clientContext != null; | ||
const preloadedQuery = (_c = (_b = clientContext === null || clientContext === void 0 ? void 0 : clientContext.preloadedQuery) !== null && _b !== void 0 ? _b : serverContext === null || serverContext === void 0 ? void 0 : serverContext.preloadedQuery) !== null && _c !== void 0 ? _c : initialPreloadedQuery; | ||
return { CSN, statusCode, preloadedQuery, err }; | ||
const preloadedQuery = (_b = (_a = clientContext === null || clientContext === void 0 ? void 0 : clientContext.preloadedQuery) !== null && _a !== void 0 ? _a : serverContext === null || serverContext === void 0 ? void 0 : serverContext.preloadedQuery) !== null && _b !== void 0 ? _b : initialPreloadedQuery; | ||
return { CSN, preloadedQuery }; | ||
} | ||
@@ -19,0 +16,0 @@ exports.getWiredProps = getWiredProps; |
import type { NextPageContext, Redirect } from 'next'; | ||
import { ErrorProps } from 'next/error'; | ||
import React, { ComponentType, ReactNode } from 'react'; | ||
import { ComponentType, ReactNode } from 'react'; | ||
import { PreloadedQuery } from 'react-relay/hooks'; | ||
import type { Environment, GraphQLTaggedNode, OperationType } from 'relay-runtime'; | ||
import { Environment, GraphQLTaggedNode, OperationType } from 'relay-runtime'; | ||
export interface WiredOptions<ServerSideProps = {}> { | ||
error?: React.ComponentType<ErrorProps & { | ||
err?: unknown; | ||
}>; | ||
fallback?: ReactNode; | ||
@@ -22,4 +18,2 @@ createClientEnvironment: () => Environment; | ||
CSN: boolean; | ||
err?: unknown; | ||
statusCode: number; | ||
preloadedQuery: PreloadedQuery<Q>; | ||
@@ -31,7 +25,3 @@ }; | ||
__wired__server__context: {}; | ||
__wired_error_context?: undefined; | ||
} | { | ||
__wired_error_context: {}; | ||
__wired__server__context?: undefined; | ||
} | { | ||
__wired__client__context?: undefined; | ||
@@ -38,0 +28,0 @@ } | { |
@@ -17,21 +17,17 @@ "use strict"; | ||
const jsx_runtime_1 = require("react/jsx-runtime"); | ||
const error_1 = __importDefault(require("next/error")); | ||
const router_1 = __importDefault(require("next/router")); | ||
const react_1 = require("react"); | ||
const hooks_1 = require("react-relay/hooks"); | ||
const relay_runtime_1 = require("relay-runtime"); | ||
const context_1 = require("./context"); | ||
const error_boundry_1 = require("./error_boundry"); | ||
// Enabling this feature flag to determine if a page should 404 on the server. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
relay_runtime_1.RelayFeatureFlags.ENABLE_REQUIRED_DIRECTIVES = true; | ||
function Wire(Component, query, opts) { | ||
function WiredComponent(props) { | ||
var _a, _b; | ||
const isError = props.err != null || | ||
`${props.statusCode}`.startsWith('4') || | ||
`${props.statusCode}`.startsWith('5'); | ||
if (isError) { | ||
const ErrorComponent = (_a = opts.error) !== null && _a !== void 0 ? _a : error_1.default; | ||
return jsx_runtime_1.jsx(ErrorComponent, { err: props.err, statusCode: props.statusCode }, void 0); | ||
var _a; | ||
if (props.CSN) { | ||
return (jsx_runtime_1.jsx(error_boundry_1.WiredErrorBoundry, { children: jsx_runtime_1.jsx(react_1.Suspense, Object.assign({ fallback: (_a = opts.fallback) !== null && _a !== void 0 ? _a : 'Loading...' }, { children: jsx_runtime_1.jsx(Component, Object.assign({}, props), void 0) }), void 0) }, void 0)); | ||
} | ||
else if (props.CSN) { | ||
return (jsx_runtime_1.jsx(error_boundry_1.WiredErrorBoundry, { children: jsx_runtime_1.jsx(react_1.Suspense, Object.assign({ fallback: (_b = opts.fallback) !== null && _b !== void 0 ? _b : 'Loading...' }, { children: jsx_runtime_1.jsx(Component, Object.assign({}, props), void 0) }), void 0) }, void 0)); | ||
} | ||
else { | ||
@@ -56,3 +52,2 @@ return jsx_runtime_1.jsx(Component, Object.assign({}, props), void 0); | ||
function getServerInitialProps(ctx, query, opts) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -73,20 +68,3 @@ const serverSideProps = opts.serverSideProps | ||
const preloadedQuery = hooks_1.loadQuery(env, query, variables); | ||
try { | ||
yield ensureQueryFlushed(preloadedQuery); | ||
} | ||
catch (e) { | ||
if (((_a = e.source) === null || _a === void 0 ? void 0 : _a.errors) != null) { | ||
const graphqlErrors = e.source.errors; | ||
const isNotFound = graphqlErrors.some((e) => e.message.startsWith('Cannot return null for non-nullable field')); | ||
if (isNotFound) { | ||
ctx.res.statusCode = 404; | ||
const context = context_1.createWiredErrorContext({ | ||
statusCode: 404, | ||
err: e.source.errors, | ||
}); | ||
return { __wired_error_context: context }; | ||
} | ||
} | ||
throw e; | ||
} | ||
yield ensureQueryFlushed(preloadedQuery); | ||
const context = context_1.createWiredServerContext({ | ||
@@ -93,0 +71,0 @@ variables, |
/// <reference types="node" /> | ||
import type { ParsedUrlQuery } from 'querystring'; | ||
import type { GraphQLTaggedNode } from 'react-relay'; | ||
import { AnyPreloadedQuery } from './types'; | ||
import type { AnyPreloadedQuery } from './types'; | ||
export interface WiredServerContext { | ||
@@ -21,3 +21,1 @@ query: GraphQLTaggedNode; | ||
} | ||
export declare function createWiredErrorContext(value: WiredErrorContext): {}; | ||
export declare function getWiredErrorContext(context: any): WiredErrorContext | undefined; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getWiredErrorContext = exports.createWiredErrorContext = exports.getWiredClientContext = exports.createWiredClientContext = exports.getWiredServerContext = exports.createWiredServerContext = void 0; | ||
exports.getWiredClientContext = exports.createWiredClientContext = exports.getWiredServerContext = exports.createWiredServerContext = void 0; | ||
const WIRED_CONTEXT = Symbol('WIRED'); | ||
@@ -41,19 +41,1 @@ function createWiredServerContext(value) { | ||
exports.getWiredClientContext = getWiredClientContext; | ||
function createWiredErrorContext(value) { | ||
const context = {}; | ||
Object.defineProperty(context, 'WIRED_CONTEXT', { | ||
enumerable: true, | ||
value: value, | ||
}); | ||
return context; | ||
} | ||
exports.createWiredErrorContext = createWiredErrorContext; | ||
function getWiredErrorContext( | ||
// Wired context values can be attached to any type. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
context) { | ||
if (context == null) | ||
return undefined; | ||
return context['WIRED_CONTEXT']; | ||
} | ||
exports.getWiredErrorContext = getWiredErrorContext; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
26675
387
251