-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from joshwcorbett/dev
- Loading branch information
Showing
7 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { PageServerLoad } from './$types' | ||
import { redirect } from '@sveltejs/kit' | ||
import { getCartId } from '$lib/utils' | ||
import type { CartCreatePayload, CartDiscountCodesUpdatePayload } from '$lib/types' | ||
import { CREATE_CART_MUTATION, DISCOUNT_CODES_UPDATE } from '$lib/server/data' | ||
|
||
export const load: PageServerLoad = async ({ params, locals, url, request, cookies, setHeaders }) => { | ||
const { storefront, locale } = locals | ||
const { code } = params | ||
const redirectTo = url.searchParams.get('redirectTo') ?? '/' | ||
let cartId = getCartId(request) | ||
const discountCodes = [code] | ||
|
||
if (!cartId) { | ||
// create a cart | ||
const { data } = await storefront.mutate<{ | ||
cartCreate: CartCreatePayload | ||
}>({ | ||
mutation: CREATE_CART_MUTATION, | ||
variables: { | ||
input: { | ||
discountCodes, | ||
country: locale?.country || undefined, | ||
language: locale?.language || undefined, | ||
}, | ||
}, | ||
}) | ||
const { cart, userErrors } = data?.cartCreate || {} | ||
|
||
if (userErrors?.length) | ||
throw redirect(302, redirectTo) | ||
|
||
cartId = cart?.id | ||
if (cartId) | ||
cookies.set('cart', `${cartId.split('/').pop()}`) | ||
} else { | ||
await storefront.mutate<{ | ||
cartDiscountCodesUpdate: CartDiscountCodesUpdatePayload | ||
}>({ | ||
mutation: DISCOUNT_CODES_UPDATE, | ||
variables: { cartId, discountCodes }, | ||
}) | ||
} | ||
|
||
// don't cache this page | ||
setHeaders({ | ||
'cache-control': 'no-store', | ||
}) | ||
|
||
throw redirect(302, redirectTo) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>Redirecting...</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { PageServerLoad } from './$types' | ||
import { POLICY_CONTENT_QUERY } from '$lib/server/data' | ||
import invariant from 'tiny-invariant' | ||
import { error } from '@sveltejs/kit' | ||
|
||
export const load: PageServerLoad = async ({ params, locals }) => { | ||
const { storefront, locale } = locals | ||
const { handle } = params | ||
|
||
const policyName = handle.replace( | ||
/-([a-z])/g, | ||
(_: unknown, m1: string) => m1.toUpperCase(), | ||
) as 'privacyPolicy' | 'shippingPolicy' | 'termsOfService' | 'refundPolicy' | ||
|
||
const { data } = await storefront.query({ | ||
query: POLICY_CONTENT_QUERY, | ||
variables: { | ||
privacyPolicy: false, | ||
shippingPolicy: false, | ||
termsOfService: false, | ||
refundPolicy: false, | ||
[policyName]: true, | ||
language: locale.language, | ||
} | ||
}) | ||
|
||
invariant(data, 'No data returned from Shopify API') | ||
|
||
const policy = data.shop?.[policyName] | ||
|
||
if (!policy) { | ||
throw error(404, 'Policy not found') | ||
} | ||
|
||
return { | ||
seo: { | ||
title: policy.title, | ||
description: policy.body, | ||
}, | ||
policy, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script lang="ts"> | ||
import type { PageServerData } from './$types' | ||
export let data: PageServerData | ||
$: ({ policy } = data) | ||
</script> | ||
|
||
<h1>{policy.title}</h1> | ||
<div>{@html policy.body}</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { json } from '@sveltejs/kit' | ||
|
||
export const GET = async (request: Request): Promise<Response> => { | ||
const sitemapUrl = undefined // TODO: this | ||
|
||
const robots = ` | ||
User-agent: * | ||
Disallow: /admin | ||
Disallow: /cart | ||
Disallow: /orders | ||
Disallow: /checkouts/ | ||
Disallow: /checkout | ||
Disallow: /carts | ||
Disallow: /account | ||
${sitemapUrl ? `Sitemap: ${sitemapUrl}` : ''} | ||
# Google adsbot ignores robots.txt unless specifically named | ||
User-agent: adsbot-google | ||
Disallow: /checkouts/ | ||
Disallow: /checkout | ||
Disallow: /carts | ||
Disallow: /orders | ||
User-agent: Pinterest | ||
Crawl-delay: 1 | ||
`.trim() | ||
|
||
return new Response(robots) | ||
} |