Skip to content

Commit

Permalink
Merge pull request #2 from joshwcorbett/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
joshistoast committed Jun 15, 2023
2 parents 617d645 + 6b166ff commit f8822d4
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/lib/server/data/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,35 @@ export const UPDATE_CART_BUYER = gql`
}
}
`

export const POLICY_CONTENT_QUERY = gql`
fragment Policy on ShopPolicy {
body
handle
id
title
url
}
query policy_query(
$language: LanguageCode
$privacyPolicy: Boolean!
$shippingPolicy: Boolean!
$termsOfService: Boolean!
$refundPolicy: Boolean!
) @inContext(language: $language) {
shop {
privacyPolicy @include(if: $privacyPolicy) {
...Policy
}
shippingPolicy @include(if: $shippingPolicy) {
...Policy
}
termsOfService @include(if: $termsOfService) {
...Policy
}
refundPolicy @include(if: $refundPolicy) {
...Policy
}
}
}
`
2 changes: 1 addition & 1 deletion src/routes/[[locale]]/cart/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const handleCartAction = async (event: Event, action: CartAction) => {
{/each}
{/if}

{#if !cart || cart?.lines?.edges.length === 0}
{#if !cart}
<p>Cart is empty</p>
{:else}
{#each cart?.lines?.edges.map((e) => e.node) as line}
Expand Down
51 changes: 51 additions & 0 deletions src/routes/[[locale]]/discounts/[code]/+page.server.ts
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)
}
1 change: 1 addition & 0 deletions src/routes/[[locale]]/discounts/[code]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Redirecting...</p>
42 changes: 42 additions & 0 deletions src/routes/[[locale]]/policies/[handle]/+page.server.ts
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,
}
}
9 changes: 9 additions & 0 deletions src/routes/[[locale]]/policies/[handle]/+page.svelte
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>
29 changes: 29 additions & 0 deletions src/routes/robots.txt/+server.ts
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)
}

0 comments on commit f8822d4

Please sign in to comment.