환경 변수를 사용하여 'NetLify에 배포'버튼을 해킹하여 사용자 정의 가능한 사이트 생성기를 만듭니다.
If you’re anything like me, you like being lazy shortcuts. The“Deploy to Netlify” buttonallows me to take this lovely feature of my personality and be productive with it.
Clicking the button above lets me (or you!) instantly clone myNext.js starter projectand automatically deploy it to Netlify. Wow! So easy! I’m so happy!
Now, as I was perusing the docs for the button the other night, as one does, I noticed that you canpre-fill environment variablesto the sites you deploy with the button. Which got me thinking… what kind of sites could I customize with that?
Idea: “Link in Bio” website
Ah, the famed “link in bio” you see all over social media when folks want you to see all of their relevant links in life. You can sign up for the various services that’ll make one of these sites for you, but what if you could make oneyourselfwithout having to sign up for yet another service?
But, we also are lazy and like shortcuts. Sounds like we can solve all of these problems with the “Deploy to Netlify” (DTN) button, and environment variables.
How would we build something like this?
In order to make our DTN button work, we need to make two projects that work together:
- A template project (This is the repo that will be cloned and customized based on the environment variables passed in.)
- A generator project (This is the project that will create the environment variables that should be passed to the button.)
I decided to be a little spicy with my examples, and so I made both projects with Vite, but the template project uses React and the generator project uses Vue.
I’ll do a high-level overview of how I built these two projects, and if you’d like to just see all the code, you can skip to the end of this post to see the final repositories!
The Template project
To start my template project, I’ll pull in Vite and React.
npm init @vitejs/app
After running this command, you can follow the prompts with whatever frameworks you’d like!
Now after doing the wholenpm installthing, you’ll want to add a.local.envfile and add in the environment variables you want to include. I want to have a name for the person who owns the site, their profile picture, and then all of their relevant links.
VITE_NAME=Cassidy Williams VITE_PROFILE_PIC=https://github.com/cassidoo.png VITE_GITHUB_LINK=https://github.com/cassidoo VITE_TWITTER_LINK=https://twitter.com/cassidoo
You can set this up however you’d like, because this is just test data we’ll build off of! As you build out your own application, you can pull in your environment variables at any time for parsing withimport.meta.env. Vite lets you access those variables from the client code withVITE_, so as you play around with variables, make sure you prepend that to your variables.
Ultimately, I made a rather large parsing function that I passed to my components to render into the template:
function getPageContent() { // Pull in all variables that start with VITE_ and turn it into an array let envVars = Object.entries(import.meta.env).filter((key) => key[0].startsWith('VITE_')) // Get the name and profile picture, since those are structured differently from the links const name = envVars.find((val) => val[0] === 'VITE_NAME')[1].replace(/_/g, ' ') const profilePic = envVars.find((val) => val[0] === 'VITE_PROFILE_PIC')[1] // ... // Pull all of the links, and properly format the names to be all lowercase and normalized let links = envVars.map((k) => { return [deEnvify(k[0]), k[1]] }) // This object is what is ultimately sent to React to be rendered return { name, profilePic, links } } function deEnvify(str) { return str.replace('VITE_', '').replace('_LINK', '').toLowerCase().split('_').join(' ') }
I can now pull in these variables into a React function that renders the components I need:
// ... return ( <div> <img alt={vars.name} src={vars.profilePic} /> <p>{vars.name}</p> {vars.links.map((l, index) => { return <Link key={`link${index}`} name={l[0]} href={l[1]} /> })} </div> ) // ...
And voilà! With a little CSS, we have a “link in bio” site!
Now let’s turn this into something that doesn’t rely on hard-coded variables. Generator time!
The Generator project
I’m going to start a new Vite site, just like I did before, but I’ll be using Vue for this one, for funzies.
Now in this project, I need to generate the environment variables we talked about above. So we’ll need an input for the name, an input for the profile picture, and then a set of inputs for each link that a person might want to make.
In myApp.vuetemplate, I’ll have these separated out like so:
<template> <div> <p> <span>Your name:</span> <input type="text" v-model="name" /> </p> <p> <span>Your profile picture:</span> <input type="text" v-model="propic" /> </p> </div> <List v-model:list="list" /> <GenerateButton :name="name" :propic="propic" :list="list" /> </template>
In thatListcomponent, we’ll have dual inputs that gather all of the links our users might want to add:
<template> <div > Add a link: <br /> <input type="text" v-model="newItem.name" /> <input type="text" v-model="newItem.url" @keyup.enter="addItem" /> <button @click="addItem">+</button> <ListItem v-for="(item, index) in list" :key="index" :item="item" @delete="removeItem(index)" /> </div> </template>
So in this component, there’s the two inputs that are adding to an object callednewItem, and then theListItemcomponent lists out all of the links that have been created already, and each one can delete itself.
Now, we can take all of these values we’ve gotten from our users, and populate theGenerateButtoncomponent with them to make our DTN button work!
The template inGenerateButtonis just antag with the link. The power in this one comes from themethodsin the
사실적인 누드 사진을 만들기 위한 AI 기반 앱 사진에서 옷을 제거하는 온라인 AI 도구입니다. 무료로 이미지를 벗다 AI 옷 제거제 완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요! 사용하기 쉬운 무료 코드 편집기 중국어 버전, 사용하기 매우 쉽습니다. 강력한 PHP 통합 개발 환경 시각적 웹 개발 도구 신 수준의 코드 편집 소프트웨어(SublimeText3)
핫 AI 도구
Undresser.AI Undress
AI Clothes Remover
Undress AI Tool
Clothoff.io
Video Face Swap
인기 기사
뜨거운 도구
메모장++7.3.1
SublimeText3 중국어 버전
스튜디오 13.0.1 보내기
드림위버 CS6
SublimeText3 Mac 버전
뜨거운 주제
7755
15
1643
14
1399
52
1293
25
1234
29

그것은#039; VUE 팀에게 그것을 끝내는 것을 축하합니다. 나는 그것이 막대한 노력과 오랜 시간이라는 것을 알고 있습니다. 모든 새로운 문서도 있습니다.

최근 Bitcoin의 가격이 20k 달러가 넘는 USD가 최근에 등반되면서 최근 30k를 끊었으므로 Ethereum을 만드는 데 깊이 다이빙을 할 가치가 있다고 생각했습니다.

나는 누군가이 매우 합법적 인 질문으로 글을 썼습니다. Lea는 브라우저에서 유효한 CSS 속성 자체를 얻는 방법에 대해 블로그를 작성했습니다. 이는 이와 같습니다.

다른 날, 나는 Corey Ginnivan의 웹 사이트에서 스크롤 할 때 카드 모음이 서로 쌓이는 것을 발견했습니다.

WordPress 편집기에서 사용자에게 직접 문서를 표시 해야하는 경우 가장 좋은 방법은 무엇입니까?

목표가 귀하의 사이트를 동시에 다른 크기로 표시하는 이러한 데스크탑 앱이 많이 있습니다. 예를 들어, 글을 쓸 수 있습니다

플렉스 레이아웃의 보라색 슬래시 영역에 대한 질문 플렉스 레이아웃을 사용할 때 개발자 도구 (d ...)와 같은 혼란스러운 현상이 발생할 수 있습니다.
