Fixing AWS Timestream query

My 'ago' function had been failing with "The query syntax is invalid" error on the following clause:

time BETWEEN ago(24h5m) AND ago(24h) AND

I fixed it by changing converting the hours to minutes:

time BETWEEN ago(1445m) AND ago(1440m) AND

reply

Other posts you might like

How I struggled to fix votes on Sanity

Ever since I implemented upvotes a few months ago, I had been struggling with user upvotes/downvotes request occasionly timing out. The bug persisted for a few months and the few times I tried to debug it, I had no success. Is it the database schema? Nope, I use similar schemas for other collections and they work fine. An inefficient MongoDB query? Same thing. No indexing? I indexed the DB even though there are barely any votes in the collection. An issue with Vercel cold start? Also not it, everything within the norm.

Last Friday the rest of the app was finally ready and I wanted to start inviting some users, so I gave up and decided to pay $20/month for Vercel Pro to increase the timeout from 10 to 60 seconds and worry about the bug another day. And then I checked the logs on Vercel Pro...

Unhandled error: MongooseError: Operation `userVotes.findOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (/var/task/sanity_client/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:175:23)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Because Mongoose timeout is 10000ms and Vercel's timeout is also 10000ms but this includes the cold start time, this error never popped up on my free plan....

sanityprogrammingvercelmongodbbuilding in public
reply

Debugging AWS Backup Error

My S3 backups, automated and on-demand, were failing with this error:

IAM Role arn:aws:iam::<role-id>:role/service-role/AWSBackupDefaultServiceRole does not have sufficient permissions to execute the backup

This happened even when using the default role, which should automatically receive the required permissions:

Eventually, I realized that the default role should receive all of the following permissions:

  • AWSBackupServiceRolePolicyForRestores
  • AWSBackupServiceRolePolicyForBackup
  • AWSBackupServiceRolePolicyForS3Backup
  • AWSBackupServiceRolePolicyForS3Restore

In my case, the last two permissions, that is AWSBackupServiceRolePolicyForS3Backup and AWSBackupServiceRolePolicyForS3Restore were, for reasons that are unknown to me, missing. I manually created these missing permissions and assigned them to a new role:

aws backupamazon web servicesprogrammings3devopsawssoftware engineering
2 comments

How I fixed @aws-crypto build error

I've been getting the following error when building my Next.js app:

Failed to compile.

./node_modules/.pnpm/@aws-crypto+sha256-js@5.2.0/node_modules/@aws-crypto/sha256-js/build/module/index.js + 12 modules Cannot get final name for export 'fromUtf8' of ./node_modules/.pnpm/@smithy+util-utf8@2.0.2/node_modules/@smithy/util-utf8/dist-es/index.js

I narrowed the source down to the following piece of code:

import { createServerRunner } from "@aws-amplify/adapter-nextjs";
import { AWS_AMPLIFY_CONFIG } from "./utils";
import { cookies } from "next/headers";
import { getCurrentUser } from "aws-amplify/auth/server";

export const { runWithAmplifyServerContext } = createServerRunner({
  config: AWS_AMPLIFY_CONFIG,
});
awsnext.js@aws-cryptoamplifyprogramming
reply

Quick feature announcement

I updated the layout a bit to make it easier to find replies to your posts and comments. This is not backward-compatible yet, so you are only going to see the posts and replies created from now on. I'll run a database migration some time in the next few days so that older replies also appear there.

sanityfeature announcementbuilding in publicux
reply

How I fixed a sticky element not working in my Next.js / Tailwind CSS app with a grid layout

I started with the following Tailwind CSS / React code and sticky positioning didn't work:

      <div className="grid grid-cols-12 gap-4 min-h-screen mt-16 pt-3">
        <main className="flex flex-col col-start-2 col-span-8">{children}</main>

        <aside className="sticky mt-14 top-20">ASIDE CONTENT</aside>
      </div>

After reading this article I realized I need to apply align-self: start; to my aside element so I added the self-start class to:

      <div className="grid grid-cols-12 gap-4 min-h-screen mt-16 pt-3">
        <main className="flex flex-col col-start-2 col-span-8">{children}</main>
next.jstailwind csscsshtmlprogrammingreactcss gridweb development
reply

How I built a chat app using Streams API, Next.JS, Redis and Vercel

Last week I added a chat feature to Sanity. In this article, I'll guide through how I built it using Streams API, Next.js, Redis and Vercel.

Sanity chat

Before we start, a quick disclaimer: there are much better ways to build a chat application, for example by using WebSockets. Vercel unfortunately doesn't support WebSockets and I didn't want to spin a dedicated server, which is why I used Streams API. Using Streams API the way I use it here is most likely not the best use of resources but it works and is a good enough solution for my small scale use. If you're on the same boat, keep reading.

If the chat takes off, I'll have to move it to a dedicated Socket.io server, a serverless WebSocket on AWS, or something similar to reduce costs.

Storing messages in Redis

I use the KV (Redis) database from Vercel to store the last 100 messages. Here is the code used to send and read messages.

import { MAX_CHAT_MESSAGE_LENGTH } from "@/utils";

const MAX_MESSAGES = 100;

export const addChatMessage = async ({
programmingvercelstreams apibackendnext.jsreactredisjavascript
reply

How to use AWS Amplify Authentication with Next.js Server Actions

No next-auth required:

// amplifyServerUtils.ts

import { createServerRunner } from '@aws-amplify/adapter-nextjs';
import config from '@/amplifyconfiguration.json';

export const { runWithAmplifyServerContext } = createServerRunner({
  config
});
// actions.ts

"use server";
import { cookies } from "next/headers";
programmingawsamplifynext.jsamplify v6cognito
reply

How I improved my sleep and my Fitbit sleep score - 12 tips 😴

At some point in my mid-to-late twenties, I realised that my sleep isn't what it used to be. I first remember noticing something isn't quite right when I got my Fitbit and consistently had higher-than-average light sleep and lower-than-average deep sleep and REM. I would often wake up early in the morning and not be able to fall back asleep. My Fitbit sleep score would be poor, hovering somewhere between 68 and 75.

After a lot of trial and error, I developed some strategies that helped me get my deep and REM sleep back to normal. The main question that I asked myself throughout this process was: If I lived in a tribal society 10,000 years ago, would this be natural for me? If the answer was yes, I'd try that change. I now have, according to Fitbit, perfectly average amounts of deep sleep and REM. My sleep score is usually somewhere between 77 and 84 and it's been steadily going up month after month. More importantly, I wake up fully rested and find it easy to get out of bed in the morning. Here are the winning techniques.

How to improve your sleep

  • Don't use an alarm clock. I know that not everyone has the same luxury but I have relatively flexible working hours so unless there's an early flight to catch, I let my body wake up when it needs to. Waking up to a jarring alarm sound elevates your stress levels - your ancestors would most likely be abruptly woken if they or their family were in physical...
fitbitsleepself improvementhealthdeep sleep
reply

some bugs with sanity

hey @tomasz, I'm noticing a few bugs with sanity. I noticed the upvotes and downvotes are not updating properly. Another issue I noticed is that when I am on my profile it is refreshing over and over again for about 10-15 seconds, I'm not sure if there is a failure to fetch problem....or something like that? might be worth looking into

<3 Radishes

1 comment
feedback