feat(server): add isPartial to context types (#1977)
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export type IsPartialInContextState = {
|
||||
setFromMiddleware: boolean;
|
||||
notSetFromMiddleware: boolean;
|
||||
};
|
||||
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext<IsPartialInContextState>,
|
||||
) {
|
||||
if (ctx.isPartial) {
|
||||
ctx.state.setFromMiddleware = true;
|
||||
} else {
|
||||
ctx.state.notSetFromMiddleware = true;
|
||||
}
|
||||
return await ctx.next();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { defineRoute, RouteConfig } from "$fresh/server.ts";
|
||||
import { IsPartialInContextState } from "./_middleware.ts";
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
export const config: RouteConfig = {
|
||||
skipAppWrapper: true,
|
||||
skipInheritedLayouts: true,
|
||||
};
|
||||
|
||||
export default defineRoute<IsPartialInContextState>((req, ctx) => {
|
||||
const result = {
|
||||
isPartial: ctx.isPartial,
|
||||
setFromMiddleware: ctx.state.setFromMiddleware,
|
||||
notSetFromMiddleware: ctx.state.notSetFromMiddleware,
|
||||
};
|
||||
if (ctx.isPartial) {
|
||||
return (
|
||||
<Partial name="slot-1">
|
||||
<pre>{JSON.stringify(result, null, 2)}</pre>
|
||||
</Partial>
|
||||
);
|
||||
}
|
||||
return <pre>{JSON.stringify(result, null, 2)}</pre>;
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
import { Handlers, PageProps, RouteConfig } from "$fresh/server.ts";
|
||||
import { IsPartialInContextState } from "./_middleware.ts";
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
export const config: RouteConfig = {
|
||||
skipAppWrapper: true,
|
||||
skipInheritedLayouts: true,
|
||||
};
|
||||
|
||||
export const handler: Handlers = {
|
||||
GET(_req, ctx) {
|
||||
return ctx.render(ctx.isPartial);
|
||||
},
|
||||
};
|
||||
|
||||
export default function Home(
|
||||
props: PageProps<boolean, IsPartialInContextState>,
|
||||
) {
|
||||
const result = {
|
||||
isPartial: props.data,
|
||||
setFromMiddleware: props.state.setFromMiddleware,
|
||||
notSetFromMiddleware: props.state.notSetFromMiddleware,
|
||||
};
|
||||
if (props.data) {
|
||||
return (
|
||||
<Partial name="slot-1">
|
||||
<pre>{JSON.stringify(result, null, 2)}</pre>
|
||||
</Partial>
|
||||
);
|
||||
}
|
||||
return <pre>{JSON.stringify(result, null, 2)}</pre>;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { Partial } from "$fresh/runtime.ts";
|
||||
|
||||
export default function SlotDemo() {
|
||||
return (
|
||||
<div>
|
||||
<div class="output">
|
||||
<Partial name="slot-1">
|
||||
<p>Default content</p>
|
||||
</Partial>
|
||||
</div>
|
||||
<a
|
||||
class="handler-update-link"
|
||||
href="/isPartial/injected"
|
||||
f-partial="/isPartial/handler"
|
||||
>
|
||||
handler update
|
||||
</a>
|
||||
<br />
|
||||
<a
|
||||
class="async-update-link"
|
||||
href="/isPartial/injected"
|
||||
f-partial="/isPartial/async"
|
||||
>
|
||||
async update
|
||||
</a>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { defineRoute } from "$fresh/src/server/defines.ts";
|
||||
|
||||
export default defineRoute(() => {
|
||||
return new Response("", {
|
||||
status: 302,
|
||||
headers: {
|
||||
Location: "/isPartial",
|
||||
},
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user