📲 Astro QR Code Generator #
In this video, we see how you can make your own, free Astro QR Code Generator. QR Codes are a convenient way to share links. Adding a QR code to your business card or merch can let potential customers get to your site just by scanning with their smartphone.
Some services offer free QR code generation but create a QR code which links to a URL shortening service. That service then forwards the visitor to your site. Unfortunately, some bad actors here stop forwarding after an initial period or demand a subscription fee. You might end up with your already distributed QR codes leading nowhere in this case. Luckily, if you know a bit about web development, we see it is pretty easy to generate your own QR codes which take visitors right to your own site.
As well as seeing how to generate QR codes, we see why you might consider using Svelte actions over an onMount function. If that doesn’t yet make sense, fear not! All shall become clear in the video. You can drop a comment below or reach out for a chat on Element as well as Twitter @mention if you have suggestions for improvements or questions.
📹 Video #
🗳 Poll #
🖥 Astro QR Code Generator: Code #
src/pages/index.astro
— click to expand code.
tsconfig.json
— click to expand code.
src/styles/fonts.css
— click to expand code.
src/styles/fonts.css
— click to expand code.
src/components/QRCode.svelte
— click to expand code.
⌨️ Astro QR Code Generator: Font Links #
🔗 Links #
- complete code in Rodney Lab GitHub repo
- create Astro app post
- using self-hosted fonts with Astro
- NPM qrcode package
- Svelte actions docs
- Element chat: #rodney matrix chat
- Twitter handle: @askRodney
🏁 Astro QR Code Generator: Summary #
How easy is it to generate your own QR codes for free? #
- Quite easy so long as you are comfortable using the Terminal or know a little about web development. We saw the latter approach in action, creating an Astro app to generate QR codes for us. We used the qrcode package, available on npm. If you know what you are doing, as an alternative, you can set this up to generate the QR code from the command line. There is also an alternative CLI tool called qrencode (available on macOS via Homebrew and also Linux). Search for it on your system package manager.
How can you avoid adding an extra API route for form submission using Astro SSR? #
- Astro lets you define API routes to handle server functions in SSR mode. Typically, you listen for form submissions on one of these API routes. To save on boilerplate code, if you only need the form data for your client code (for example, if you do not have to store it to your database) you can set the form method to GET. You need to remember to name all the inputs and set the form action to an empty string. Now when you submit the form, the browser goes to the same URL as the form page, but now you will have the form inputs appended to the URL as URL search parameters. They will be URI encoded. Now you can update the interface with the submitted data, using `Astro.request.url` to access the URL and then parse the search parameters.
Why use Svelte Actions instead of onMount? #
- Svelte actions and onMount can each be used if you want to manipulate or mutate a DOM node. We saw an example where we painted a QR code to an HTML canvas node, but you also might want to add event listeners or update the node’s styles. Svelte actions use a little less boilerplate code than an onMount method. Besides this, they are easier to share between components. Finally, they have a mechanism for updating in-built. This saves you have to add an extra beforeUpdate or similar.
🙏🏽 Feedback #
Have you found the post useful? Would you prefer to see posts on another topic instead? Get in touch with ideas for new posts. Also, if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.
Just dropped a new video on how you create QR codes using 🚀 Astro SSR and ❤️ Svelte actions.
— Rodney (@askRodney) February 6, 2023
Hope you find it useful!
#learnastro #useThePlatform #JAMStack pic.twitter.com/fydivqJUDq
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter, @[email protected] on Mastodon and also the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Astro as well as SEO. Also, subscribe to the newsletter to keep up-to-date with our latest projects.